How to parse nested JSON input to object into jersey?

I am passing input to my API like this:

{
  "param1": "value1",
  "param2":"value2",
  "param3": {
               "min": "xyz",
               "max": "abc"
            }
}

I want to parse this JSON request into object like :

class Filter {
       String param1;
       String param2;
       //frequency object will consist of maximum and minimum attributes
       Frequency frequency

}

How it is possible to automatically instantiate filter object in my API method?

Thanks in advance.

Answers


You can use jackson mapper

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.7.2</version>
</dependency>

or if You using Jersey instead

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.22.2</version>
</dependency>

ObjectMapper mapper = new ObjectMapper();
String jsonString = "{\"param1\":\"Bar\",\"param2\":\"Foo\",\"frequency\":{\"min\":0,\"max\":10}}";
Filter filter = mapper.readValue(jsonString, Filter.class);
System.out.println(mapper.writeValueAsString(filter));

public class Filter {
    public String param1;
    public String param2;
    public Frequency frequency;
}

public class Frequency {
    public int min;
    public int max;
}

Need Your Help

List global variables in executable

c xcode macos gcc otool

How can I dump all the global variables and the address offsets in my executable?

Can I use redux-saga's es6 generators as onmessage listener for websockets or eventsource?

reactjs ecmascript-6 redux redux-saga

I'm trying to get redux-saga working with the onmessage listener. I don't know why what I have isn't working.