简体   繁体   中英

jersey consume two dimensional array

I am developing REST webservices using jersey.

I am submitting the data in following format:

var data = new Array();
data.push({ name: 'field1', value: "123" });
data.push({ name: "field2", value: "XYZ" });

Now I want to send additional data in following format:

    var products = new Array();
    products.push({ name: 'product1', value: "5" });
    products.push({ name: 'product2', value: "8" });
    products.push({ name: 'product3', value: "12" });

    data.push({ name: "products", value: products });

Jersey webservice code:

@Path("/myRequestPath")
@POST
@Produces({ MediaType.APPLICATION_JSON })
public MetaDataStatus processChangeRequest(
    @FormParam("field1") String field1,
    @FormParam("field2") String field2,
          //how to read products here?
    )
{
}

My question in that how to consume above products data in jersey web service?

Don't send the data in this format. Use objects that easily mappable to Java objects. First thing you need to understand is how your data looks in JSON and will map to Java objects.

First you have an array var data = new Array(); . So initially you have the following in JSON

[]

which just an empty array. In the Java JSON/POJO mapping world, this generally maps to a Java List or plain java array, so you simply have as of now

List data = new ArrayList();

Then you start adding Javascript objects ( { } ), which map to JSON objects ( { } ).

So after the first two adds, you have

[
  { name: 'field1', value: "123" },
  { name: 'field2', value: "XYZ" }
]

But JSON objects ( {..} ) map to Java objects. So we would need a class to hold name and value fields. For instance

public class KeyValuePair {
    public String name;
    public String value;
}

So now you have something that maps to

List<KeyValuePair> data = new ArrayList<>();
data.add(new KeyValuePair("field1", "123"));
data.add(new KeyValuePair("field2", "XYX"));

As you can see it's beginning to look pretty ugly. This is because of the way you are sending the data. But as you can see from the KeyValuePair example, the Javascript properties, ie name and value map to the Java fields name and value . So what you should do instead is just forget the name value pairs, and simply map the Javacript object straight to the Java object. For example

var obj = {
    field1: "123",
    field2: "XYZ",
    products: [
        { name: "product1", price: 10 },
        { name: "product2", price: 20 },
        { name: "product2", price: 30 }
    ]
};
var data = JSON.stringify(obj);
$.ajax({
    url:url,
    data: data,
    contentType: "application/json",
    ...
});

So now the Javacript obj would map to Java like

public class MyObject {
    public String field1;
    public String field2;
    public List<Product> products;
    // should actually be private with getters and setters
}

public class Product {
    public String name;
    public double price;
}

I hope you can see how the Javascript maps directly to the Java class. So now you can simply have this as the method

@Path("/myRequestPath")
@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
public MetaDataStatus processChangeRequest(MyObject obj) {}

For this to work though, you need to make sure you have a JSON provider for Jersey. If you don't have one, you will get an exception with something like "MessageBodyReader not found for media type application/json and Java type MyObject" . If you see this exception, you can do a google search with the exception messag. There are so many answers that show what you need to add. I could tell you myself, but you have not provided enough information to where I could tell you exactly what you need.

If you need help figuring it out, please let me know what version of Jersey you are using and if you are using Maven or not.

UPDATE

For Maven, just add this dependency for the JSON/POJO support

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.7</version>
</dependency>

Then configure it in your web.xml

<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>

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