[英]how to send JS property Array to Jersey JAX-RS JSON based REST web service
I need to send an array of JS strings to the server side, as part of a field of a JS Object such as: 我需要将JS字符串数组发送到服务器端,作为JS Object字段的一部分,例如:
JSON: JSON:
{prodName: "abc123", prodImages: ["a1", "a2", "a3"]}
I am using Jersey JAX-RS to consume the JSON input. 我正在使用Jersey JAX-RS来使用JSON输入。 In my server side code I have: 在我的服务器端代码中,我有:
Web service method signature: Web服务方法签名:
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("/delete")
public void deleteMethod(ProdListVO prodListDeleteVO) //...
JAX-B Object: JAX-B对象:
@XmlRootElement
public class ProdListVO {
private String prodName;
private String[] prodImages;
// ... getters and setters
prodName is correctly set while prodImages is null. prodImages为null时,正确设置了prodName。 Are there any proper JSON formats that Jersey understands as a valid Array or List of String ? Jersey是否将任何正确的JSON格式理解为有效的数组或字符串列表?
Try using: 尝试使用:
{ "prodName": "abc123", "prodImages": ["a1", "a2", "a3"]} {“ prodName”:“ abc123”,“ prodImages”:[“ a1”,“ a2”,“ a3”]}
also include following dependency: 还包括以下依赖项:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
How about: 怎么样:
RestClass: RestClass:
@DELETE
@Consumes(MediaType.APPLICATION_XML)
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("/delete")
public Response callMokuSimple(final JAXBElement<ProdListVO> prodListDeleteVO) {
ProdListVO prodList = prodListDeleteVO.getValue();
// more code
}
JAXBClass: JAXBClass:
@XmlAccessorType(XmlAccessType.FIELD)//lets you decide the fields
@XmlRootElement(name="algorithm")
public class Algorithm implements Serializable{
private static final long serialVersionUID = -1L;
@XmlElement(name="prodName")//name of field
private String prodName = null;
@XmlElement(name="prodImages")//name of other field
private String [] prodImages = null;
//getter and setters
}
after upgrading EclipseLink JAXB Moxy to version 2.6 it solved the problem as based on solution of question: 将EclipseLink JAXB Moxy升级到2.6版后,它根据问题的解决方案解决了该问题:
Moxy, JSON and Jersey 2.0 does not deserialize plain String array Moxy,JSON和Jersey 2.0不会反序列化纯字符串数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.