简体   繁体   中英

Web Service accepting Object[]. What JSON it accepts?

I am writing a web service which should accept type Object[]. Its universal and needs to accept different number and types of parameters in different scenarios.

Request object looks like this:

@XmlRootElement
public class SimilarityRequest {

    private Object[] params;
    private String similarity;

    public Object[] getParams() {
    return params;
    }

    public void setParams(Object[] params) {
    this.params = params;
    }  

    public String getSimilarity() {
    return similarity;
    }

    public void setSimilarity(String similarity) {
    this.similarity = similarity;
    }

}

This is WebService:

@SessionScoped
@Path("/similarity/")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Stateful
@StatefulTimeout(600000) // 10 minutes
public class SimilarityResource {


    @POST
    @Path("/")
    public List<SimilarityResult> universalSimilarity(JAXBElement<SimilarityRequest> sr) {

        Object[] params = sr.getValue().getParams();
        String similarity = sr.getValue().getSimilarity();

    }


}

I dont know what json it accepts for params in this case? I tried "params":{5,10} and "params":{"0":5,"1":10} and also "params":[5,10] . Something throws 500 and something 400 (bad request). Any ideas?

I've successfully implemented the service using Jersey, the code is the same, I've just removed the JAXBElement wrapper and the @XmlRootElement annotation. The WEB-INF.xml file must include the folder containing the SimilarityRequest class in the com.sun.jersey.config.property.packages parameter section and the com.sun.jersey.api.json.POJOMappingFeature parameter must be true. The service receives correctly the following json:

{ "similarity": "test", "params":[5,10] }

The object array contains two Integer values.

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