Jersey POST Method is receiving null values as parameters

后端 未结 4 1151
情话喂你
情话喂你 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:39

    I just started developing webservice in java and had a same problem with POST data. I got very simple solution to read the POST data using @FormParam, Actually i was using the @QueryParam to read POST data, I think its only for reading the QueryString data using GET method

    A very nice documentation given here. Having read this article, most of my confusions were cleared. http://docs.jboss.org/resteasy/docs/2.0.0.GA/userguide/html_single/index.html

    Tip: Just make sure you are using "application/x-www-form-urlencoded" mime type while using @FormParam

    0 讨论(0)
  • 2020-12-09 05:40

    Assume we have a tenant object which has id and name attributes and REST resource is exposed via @POST and @PATH("/xyz/tenants").

    Body example in JSON:

    {"id":"001","name":"myname"}
    

    Body example in XML:

    <tenant><id>001</id><name>myname</name></tenant>
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-09 05:59

    I'd post this as a comment to the accepted answer, but I'm just shy of being able to do that.

    In addition to the excellent advice above, I would add that, at least in version 2.0.x, Jersey does not pull @FormParam from the query string. Instead, it expects it to be included as a name-value pair in the request body.

    For example, instead of POST http://localhost/app?name=Joe, you would send POST http://localhost/app with the body:

    name=Joe
    
    0 讨论(0)
提交回复
热议问题