Jax-Rs Post Multiple Objects

JAX-RS Post multiple objects

The answer is no.

The reason is simple: This about the parameters you can receive in a method. They must be related to the request. Right? So they must be either headers or cookies or query parameters or matrix parameters or path parameters or request body. (Just to tell the complete story there is additional types of parameters called context).

Now, when you receive JSON object in your request, you receive it in a request body. How many bodies the request may have? One and only one. So you can receive only one JSON object.

JAX-RS Post method with multiple parameters

for RESTful API, you should not be relay on the usual web application parameter passing style,

... URL..?param=value

you should form a url in a way that, it make sense to access the resource:

for example:

@POST
@Path("/{courseId}/subjects/{"subjectId"}/description")
public Message get(@PathParam("courseId") String courseId,
@PathParam("subjectId") String subjectId) {
// ....
}

this resource endpoint is giving a way to post a new description for a specific subject under a specific course. So this is how you could access multiple Path parameters in the same Post request.

On the other hand, if you are talking about how to get value of all fields on your 'User' class then you should consider annotating the class with @XmlRootElement

@XmlRootElement
public class User{
int id;
String name;
String pass;

//empty contractors is mandatory in addition to the public getter and
// setters
public User(){
}

// here are the getter setter and constructors

}

now if you send with a POST method something like below : [JSON format] :

{
"id":"123"
"name":"user name"
"pass":"pass"
}

jersey will take of creating instance of User class with the data in the body of the request. which is the reason why you will need mandatory empty constructor in your User class, jersey will first create the instance of the class using the empty constructor and calls setters of each field to set values.

with that in place, if you simple put User in parameter of your method you will have object passed to your method.

@POST
@Path ("/addData")
@produce(MediaType.Application_Json)
@Consume(MediaType.Application_JSON)
public User addData(User newUser){
//do something
return newUser;
}

Passing Two Objects in Rest Api using Jersey

That is not possible. See the JAX-RS specification:

3.3.2.1 Entity Parameters

The value of a parameter not annotated with @FormParam or any of the annotations listed in in Section 3.2, called the entity parameter, is mapped from the request entity body. Conversion between an entity body and a Java type is the responsibility of an entity provider, see Section 4.2. Resource methods MUST have at most one entity parameter.

There can be only one method 'entity parameter'.

What you ask for would not be RESTful. REST ist not RPC (Remote Procedure Call), you don't 'pass' objects to a 'method'. In REST you transfer Resource representations from and to identifying URLs.

In your example the Resource would be an EmployeeInfo wrapping Employee and Manager.

Besides, /post is not a very RESTful URL. What Resource is identified by this? What happens if you GET /post? Please think in REST terms, not in RPC.

multiple parameters in web service java JAX-RS

Resource methods may not have more than one entity parameter. You can have multiple @PathParam, @QueryParam, etc. but only one unannotated parameter in each resource method.

3.3.2.1 Entity Parameters
The value of a parameter not annotated with
@FormParam
or any of the annotations listed in in Section 3.2,
called the entity parameter, is mapped from the request entity body. Conversion between an entity body and
a Java type is the responsibility of an entity provider, see Section 4.2. Resource methods MUST have at
most one entity parameter.

http://download.oracle.com/otn-pub/jcp/jaxrs-2_1-final-eval-spec/jaxrs-2_1-final-spec.pdf

You could remove UserMaker user from your resource method and instead pass the user id to userManagementWS.hireBook(idU, book). And then retrieve the user from your Map<Long, UserMaker> via userMaker.get(idU).
https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#get-java.lang.Object-

But I'd recommend you restructure your api. I found this link pretty informative http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api.



Related Topics



Leave a reply



Submit