java 如何在 Apache CXF jax-rs (REST) 中提交 JSON 数据作为请求正文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5844394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to submit JSON data as request body in Apache CXF jax-rs (REST)
提问by Arnav
I am using Apache-CXF for creating REST web services and trying to submit a form.
我正在使用 Apache-CXF 创建 REST Web 服务并尝试提交表单。
Server:
This is my method, which is expected to get json data.
服务端:
这是我的方法,期望得到json数据。
@POST
@Path("/addCustomer/")
@Consumes(MediaType.APPLICATION_JSON)
//{"Customer":{"name":"Some Name","id":6}}
public Customer addCustomer(Customer customer){
logger.debug(customer);
return customer;
}
Client: I am using firefox REST plugin for submitting request: Using REST client, I have posted following json as request body:
客户端:我使用 firefox REST 插件提交请求:使用 REST 客户端,我发布了以下 json 作为请求正文:
{"Customer":{"name":"Arnav Awasthi","id":6}}
But I am getting "415: Unsupported Media Type"
.
但我得到"415: Unsupported Media Type"
.
回答by MBK
use restclient , a plugin for fire fox and add the http headers as Accept:application/json ,content-type: application/json.
使用 restclient ,一个 firefox 插件,并将 http 头添加为 Accept:application/json ,内容类型:application/json。
回答by FabienM
Sorry for the late answer, but it may serve to others.
抱歉回复晚了,但它可能对其他人有用。
You should doublecheck than your Customer class is annotated with JAXB's @XmlRootElement, since Hymanson needs it to deserialize JSON message.
您应该仔细检查您的 Customer 类是否使用 JAXB 的 @XmlRootElement 进行注释,因为 Hymanson 需要它来反序列化 JSON 消息。
回答by kolchanov
I had the same error some time ago. It seems the root reason was exception "No message body reader has been found for request class ".
前段时间我遇到了同样的错误。似乎根本原因是异常“未找到请求类的消息正文阅读器”。
According to http://www.javatips.net/blog/2012/02/cxf-restful-tutorialI added jettison library to resolve this issue.
根据http://www.javatips.net/blog/2012/02/cxf-restful-tutorial我添加了 jettison 库来解决这个问题。
回答by bmargulies
You have to find a way to tell firefox to set the content-type to application/json. The error indicates that it's sending something else.
您必须找到一种方法来告诉 Firefox 将内容类型设置为 application/json。该错误表明它正在发送其他内容。
回答by Rohan
I faced the same issue using CXF 2.7.4 with Jasckon 2.X.X . But it was fixed when i upgraded to CXF 2.7.7 . Or use Hymanson 1.9.X with CXF 2.7.4 .
我在使用 CXF 2.7.4 和 Jasckon 2.XX 时遇到了同样的问题。但是当我升级到 CXF 2.7.7 时它被修复了。或者将 Hymanson 1.9.X 与 CXF 2.7.4 一起使用。
回答by Pubudu Dodangoda
I had the same problem. The solution was to remove the bean class name from the json string. In your case, the Json which should be sent as the body would be,
我有同样的问题。解决方案是从 json 字符串中删除 bean 类名。在您的情况下,应该作为正文发送的 Json,
{"name":"Arnav Awasthi","id":6}
回答by Krishna Tula
You have to add custom headers to inform the client what kind of data you are sending back e.g: Header Name: Content-type Header-Value : application/json
您必须添加自定义标头以通知客户端您要发回的数据类型,例如:标头名称:内容类型标头值:应用程序/json