robin
robin

Reputation: 1925

Issue with rest Service using rest easy

I have a complex object like below

public class TestFilter {

    public TestFilter(){

    }

    private Set<String> m_categories;

    private Set<String> m_taskNames;

    public Set<String> getCategories() {
        return m_categories;
    }

    public void setCategories(Set<String> categories) {
        this.m_categories = categories;
    }

    public Set<String> getTaskNames() {
        return m_taskNames;
    }

    public void setTaskNames(Set<String> taskNames) {
        this.m_taskNames = taskNames;
    }


    public static TestFilter fromString(String jsonRepresentation){
        ObjectMapper mapper = new ObjectMapper();
        TestFilter filter= null;
        try {
            filter = mapper.readValue(jsonRepresentation, TestFilter.class );
        } catch (IOException e) {
            throw new MyException("Exception while parsing the TestFilter");
        }
        return filter;
    }
}

I am using it in my rest service like below

@GET
    @Path("/schedule/info")
    public Response getScheduledTasks(@QueryParam(FILTERS)TestFilter testFilter){
        if(testFilter == null){
            System.out.println("its null");
        } else {
            System.out.println("not null");
        }
        return Response.status(Status.OK).entity("Success").build();
    }

The url I am using for accessing the object is like below.The url is decoded for ease of reading.

https://myip:port/my-context/rest/schedule/info?filters="{"categories":["C","D"],"taskName":["TaskA"]}"

I had put a debug point on the service, so its hitting the service but the problem is that testFilter is always coming as null.

Please let me know what is the issue with the code.

Upvotes: 1

Views: 196

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209004

JSON wrapped in " is just a JSON String. A JSON object should not have the quotes. So just just unwrap the JSON from the quotes

filters={"categories":["C","D"],"taskNames":["TaskA"]}

Another thing, based on your comments. If you want to avoid a 404 NotFound, if you want to change it to something else like 400 Bad Request, then just throw that instead

throw new BadRequestException()
// or new WebApplicationException(400)

It's odd to me that bad query parameters would result in a 404, but this is the specified behavior with JAX-RS. Personally, I just change it to 400 in cases like this.

Upvotes: 1

Related Questions