休息问题使用休息轻松

时间:2021-08-30 07:29:09

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.

我用来访问该对象的URL如下所示。为了便于阅读,URL被解码。

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.

我在服务上放了一个调试点,所以它命中了服务,但问题是testFilter总是变为null。

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

请告诉我代码的问题。

1 个解决方案

#1


1  

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

包含在JSON中的JSON只是一个JSON字符串.JSON对象不应该有引号。所以只需从引号中解开JSON

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

另一件事,根据你的意见。如果你想避免404 NotFound,如果你想把它改成400 Bad Request这样的东西,那么就把它扔掉

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.

对我来说奇怪的是,错误的查询参数会导致404,但这是JAX-RS的指定行为。就个人而言,我只是在这种情况下将其更改为400。

#1


1  

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

包含在JSON中的JSON只是一个JSON字符串.JSON对象不应该有引号。所以只需从引号中解开JSON

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

另一件事,根据你的意见。如果你想避免404 NotFound,如果你想把它改成400 Bad Request这样的东西,那么就把它扔掉

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.

对我来说奇怪的是,错误的查询参数会导致404,但这是JAX-RS的指定行为。就个人而言,我只是在这种情况下将其更改为400。