如何将 JSON 对象发布到 JAX-RS 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7693669/
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 POST a JSON object to a JAX-RS service
提问by Naresh
I am using the Jersey implementation of JAX-RS. I would like to POST a JSON object to this service but I am getting an error code 415 Unsupported Media Type. What am I missing?
我正在使用 JAX-RS 的 Jersey 实现。我想向此服务发布 JSON 对象,但收到错误代码 415 不支持的媒体类型。我错过了什么?
Here's my code:
这是我的代码:
@Path("/orders")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class OrderResource {
private static Map<Integer, Order> orders = new HashMap<Integer, Order>();
@POST
public void createOrder(Order order) {
orders.put(order.id, order);
}
@GET
@Path("/{id}")
public Order getOrder(@PathParam("id") int id) {
Order order = orders.get(id);
if (order == null) {
order = new Order(0, "Buy", "Unknown", 0);
}
return order;
}
}
Here's the Order object:
这是订单对象:
public class Order {
public int id;
public String side;
public String symbol;
public int quantity;
...
}
A GET request like this works perfectly and returns an order in JSON format:
像这样的 GET 请求完美地工作并以 JSON 格式返回订单:
GET http://localhost:8080/jaxrs-oms/rest/orders/123 HTTP/1.1
However a POST request like this returns a 415:
但是,像这样的 POST 请求会返回 415:
POST http://localhost:8080/jaxrs-oms/rest/orders HTTP/1.1
{
"id": "123",
"symbol": "AAPL",
"side": "Buy",
"quantity": "1000"
}
回答by Naresh
The answer was surprisingly simple. I had to add a Content-Typeheader in the POSTrequest with a value of application/json. Without this header Jersey did not know what to do with the request body (in spite of the @Consumes(MediaType.APPLICATION_JSON)annotation)!
答案出奇的简单。我必须Content-Type在POST请求中添加一个值为application/json. 没有这个标头,Jersey 不知道如何处理请求体(尽管有@Consumes(MediaType.APPLICATION_JSON)注释)!
回答by glm
Jersey makes the process very easy, my service class worked well with JSON, all I had to do is to add the dependencies in the pom.xml
Jersey 使这个过程非常简单,我的服务类与 JSON 配合得很好,我所要做的就是在 pom.xml 中添加依赖项
@Path("/customer")
public class CustomerService {
private static Map<Integer, Customer> customers = new HashMap<Integer, Customer>();
@POST
@Path("save")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public SaveResult save(Customer c) {
customers.put(c.getId(), c);
SaveResult sr = new SaveResult();
sr.sucess = true;
return sr;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}")
public Customer getCustomer(@PathParam("id") int id) {
Customer c = customers.get(id);
if (c == null) {
c = new Customer();
c.setId(id * 3);
c.setName("unknow " + id);
}
return c;
}
}
And in the pom.xml
在 pom.xml 中
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-Hymanson</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.7</version>
</dependency>
回答by Fabiano Tarlao
I faced the same 415http error when sending objects, serialized into JSON, via PUT/PUSH requests to my JAX-rs services, in other words my server was not able to de-serialize the objects from JSON.
In my case, the server was able to serialize successfully the same objects in JSON when sending them into its responses.
在415通过 PUT/PUSH 请求向我的 JAX-rs 服务发送序列化为 JSON 的对象时,我遇到了相同的http 错误,换句话说,我的服务器无法从 JSON 反序列化对象。就我而言,服务器能够在将 JSON 中的相同对象发送到其响应中时成功地序列化它们。
As mentioned in the other responses I have correctly set the Acceptand Content-Typeheaders to application/json, but it doesn't suffice.
正如其他回复中提到的,我已将Accept和Content-Type标头正确设置为application/json,但这还不够。
Solution
解决方案
I simply forgot a default constructor with no parameters for my DTO objects. Yes this is the same reasoning behind @Entity objects, you need a constructor with no parameters for the ORM to instantiate objects and populate the fields later.
我只是忘记了我的 DTO 对象没有参数的默认构造函数。是的,这与@Entity 对象背后的推理相同,您需要一个没有参数的构造函数,ORM 才能实例化对象并稍后填充字段。
Adding the constructor with no parameters to my DTO objects solved my issue. Here follows an example that resembles my code:
将没有参数的构造函数添加到我的 DTO 对象解决了我的问题。下面是一个类似于我的代码的示例:
Wrong
错误的
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class NumberDTO {
public NumberDTO(Number number) {
this.number = number;
}
private Number number;
public Number getNumber() {
return number;
}
public void setNumber(Number string) {
this.number = string;
}
}
Right
对
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class NumberDTO {
public NumberDTO() {
}
public NumberDTO(Number number) {
this.number = number;
}
private Number number;
public Number getNumber() {
return number;
}
public void setNumber(Number string) {
this.number = string;
}
}
I lost hours, I hope this'll save yours ;-)
我失去了几个小时,我希望这能拯救你的时间;-)

