Java – @ get can define consumption content type for jax-rs implementation?

I've been trying some examples on jaxrs (Jersey in this case) Here is my example stub implementation:

@Path("stubservice")
public class StubImpl
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String getString(@QueryParam("first")
    int first,@QueryParam("second")
    int second)
    {
        return "first: " + first + " second: " + second;
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getSize(@QueryParam("size")
                              int size,@Context
                              HttpHeaders headers)
    {
        Gson gson = new Gson();
        return gson.toJson("something else");
    }
}

If there is no getSize method with @ consumers (mediatype. Application_json) in its definition, this class has an error during initialization But with it, the stubbimpl class initializes correctly and processes the request according to whether the incoming request uses its content type as application / JSON

An error occurred during initialization:

Critical: the following errors and warnings have been detected using resource and / or provider classes: critical: a media type conflict has occurred Resource method public Java lang.String StubImpl. GetString (int, int) and public Java lang.String StubImpl. GetSize (int, javax. WS. Rs.core. Httpheaders) can generate the same media type

As far as I know, @ consumption (mediatype. Application_json) is never required for @ get request, because it is applicable to the content type in the body (and the get method has no body)

Is the existing behavior promising?

Thank you in advance

Solution

Not a Jax - RS expert at all, so it's just a guess

If you do not set @ consumers (mediatype. Application_json), how does Jersey decide which method to call when a get request enters?

Both methods respond to get requests, accept any media type on the same path, and generate the same media type So my guess is that when a get request enters this path, Jersey cannot decide which method to call (except randomly), so it refuses to start

@The consumption annotation causes it to call getSize when the request has a JSON body (that is, never), and another method in all other cases (that is, always)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>