英文:
Eclipse JAXRS POST Request payload not converted into request model object
问题
以下是翻译好的内容:
我在POST请求体中发送了一个JSON数据载荷,如下所示:
{
"id": "xyz3",
"model": "Camry",
"year": 2010
}
我希望这个数据载荷被转换为我定义的Java类CarRequest的实例,如下所示:
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
...
@XmlRootElement(name = "carRequest")
@XmlAccessorType(XmlAccessType.FIELD)
public class CarRequest {
private String id;
private String model;
private int year;
public CarRequest() {
}
public String getId() {
return id;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
}
}
然而,在我的POST端点资源方法中,我没有看到这种情况发生。我的资源定义如下:
@POST
@Path("cars")
@Produces({ "application/json", "application/xml" })
public Response carsInfos(@Valid CarRequest carRequest) {
System.out.println(carRequest); //打印 { "id": "null", "model": "null", "year": 0 }
return Response.ok().build();
}
上面,打印carRequest的那一行输出结果如下:
{ "id": "null", "model": "null", "year": 0 }
而不是像下面这样:
{ "id": "xyz3", "model": "Camry", "year": 2010 }
英文:
I have a JSON payload sent as part of POST request body like:
{
"id": "xyz3",
"model": "Camry",
"year": 2010
}
And I would expect this payload to be converted to the instance of my java class CarRequest defined like:
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
...
@XmlRootElement(name = "carRequest")
@XmlAccessorType(XmlAccessType.FIELD)
public class CarRequest {
private String id;
private String model;
private int year;
public CarRequest() {
}
public String getId() {
return id;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
}
}
However, in my POST endpoint resource method I dont see this happening. My resource is defined as:
@POST
@Path("cars")
@Produces({ "application/json", "application/xml" })
public Response carsInfos(@Valid CarRequest carRequest) {
System.out.println(carRequest); //prints { "id": "null", "model": "null", "year": 0 }
return Response.ok().build();
}
Above, the line that prints carRequest prints it like
{ "id": "null", "model": "null", "year": 0 }
, instead of like
{ "id": "xyz3", "model": "Camry", "year": 2010 }
答案1
得分: 1
我只用过Jackson注解来指定映射,但我认为真正的问题可能是缺少一个"@Consumes"注解。因为这个原因,我相信它不知道如何解析你的请求主体。
英文:
I've only ever used the Jackson annotations for specifying mappings, but I think the real problem here may be the lack of a "@Consumes" annotation. Because of that, I believe it doesn't know how to parse your request body.
答案2
得分: 0
我尝试过@Consumes注解(感谢@David M. Karr),但那并没有解决任何问题。原来我的服务器在使用JavaEE 8和JAXRS 2.1功能。在添加了JavaEE7和JAXRS 2.0的功能后,请求模型开始填充了值。
英文:
I have tried @Consumes annotation (thanks @David M. Karr) but that did not solve anything. It turns out that my server was using JavaEE 8 with JAXRS 2.1 features.
After adding features for JavaEE7 and JAXRS 2.0, the request model started getting populated with the values.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论