Jersey POST Method is receiving null values as parameters

后端 未结 4 1167
情话喂你
情话喂你 2020-12-09 05:18

I am developing RESTful services with Jersey and it works great with GET methods. However I can\'t make it work with POST methods and JSON or text parameters. These is what

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 05:48

    A path parameter is a portion of the request URL matching a particular pattern. As such, there are character limitations on what can be specified as a path param - in particular any special characters need to be URL encoded. This applies the same across any kind of request (GET, POST, PUT, DELETE).

    As a general rule, you should restrict your path parameters to simple values like identifiers, or resource endpoints - more complex data should be passed to the REST service via request parameters or the request body itself. Here's a mixed approach that passes an entity identifier as a path parameter, and the entity data in the request body:

    @Path("/contacts/{id}")
    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response updateContact(@PathParam final String contactId, Contact contact) {
    }
    

    In the above example, the contactId is obtained as a path parameter and the contact is serialized automatically from the request body.

    What I described above are general rules. Now as to the specifics of your case, one thing I notice in your code is that you don't actually define any path parameters. Remember that they have to be defined as part of your @Path annotation, before being consumed in your REST method:

    @Path("/method/{obj1}/{obj2}")
    public ResponseObject method(@Context Request request, @PathParam("obj1") Object obj1, @PathParam("obj2") String obj2) {
    }
    

    With the above changes your parameters should no longer show up as being null, assuming you have properly encoded the URL on the client side.


    * EDIT *

    Based on your comment, I see you need to become more familiar with the JAX-RS specification and the various parameter types. I recommend reading through the RESTEasy JAX-RS Documentation. It has some vendor specific implementation details but all in all is an excellent guide to JAX-RS.


    @PathParam

    Purpose: Used to inject a portion of the request URL into a variable. Note that URL parameters are not considered part of the URL.

    Example: Given the URL http://services.example.com/contacts/20578, I can define:

    @Path("/contacts/{id}")
    

    From which I can inject a @PathParam("id").

    public Response getContact(@PathParam("id") final String identifier);
    

    This works for any kind of HTTP request (GET, POST, PUT, DELETE).


    @QueryParam

    Purpose: Used to inject a portion of the query string or form encoded data into a variable. The query string is that portion of your URL after the ?. Form encoded data are the URL encoded name/value pair data passed in the body of an HTTP request, when the request type is application/x-www-form-urlencoded. Typically, query parameters are passed as part of the URL string for GET requests, and in the request body for POST requests.

    Example: Given the URL http://services.example.com/contacts?group=Business, I can inject a @QueryParam("group")

    public Response getContactsInGroup(@QueryParam("group") final String groupName);
    

    It's atypical to use query parameters with a POST request, but it is possible if the request type is application/x-www-form-urlencoded:

    @POST
    @Path("/contacts")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Response createContact(@QueryParam("contact") final Contact contactData, @QueryParam("metadata") final String metaData);
    

    These are just high level examples, please read through the documentation I linked to get a better example of how each parameter type works, and when to use which one.

提交回复
热议问题