Eclipse JAX-RS POST请求有效载荷未转换为请求模型对象。

huangapple go评论72阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年10月27日 02:04:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64542604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定