简体   繁体   中英

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:

JSON:

{prodName: "abc123", prodImages: ["a1", "a2", "a3"]}

I am using Jersey JAX-RS to consume the JSON input. In my server side code I have:

Web service method signature:

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("/delete")
public void deleteMethod(ProdListVO prodListDeleteVO) //...

JAX-B Object:

@XmlRootElement
public class ProdListVO {   

    private String prodName;
    private String[] prodImages;

// ... getters and setters

prodName is correctly set while prodImages is null. Are there any proper JSON formats that Jersey understands as a valid Array or List of String ?

Try using:

{ "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:

@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:

@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:

Moxy, JSON and Jersey 2.0 does not deserialize plain String array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM