Stark
Stark

Reputation: 481

JAX-RS Post method with multiple parameters

I want to make a JAX-RS web service using jersey implementation. I need a post method with 3 parameters.

@POST
@Path ("/addData")
@produce(MediaType.Application_Json)
@Consume(MediaType.Application_JSON)
public User addData(int id, String name, String pass){
    User u = new User();
    u.setId(id);
    u.setName(name);
    u.setPass(pass);
    return u;
}

@POST
@Path ("/addData")
@produce(MediaType.Application_Json)
@Consume(MediaType.Application_JSON)
public User addSingleData(int id){
    User u = new User();
    u.setId(id);
    return u;
}

There is a separate User class as follow:

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

    // here are the getter setter and constructors

}

First, can I use jersey-media-moxy-2.3.jar file for conversion to JSON (i dont want to use maven). because this jar file is not converting content to json but if i use maven its working fine without parameters in post method.

second, how to get param in body of method if i am using only one param. i.e. second method

third, how to use multiple params in post method.

fourth, in further i need to upload and download an image. how to do that.

last, i am not able to get data in json format.

NOTE: I am making web service for android mobile. i am going to consume it via andorid.

Upvotes: 1

Views: 10206

Answers (1)

LeTex
LeTex

Reputation: 1452

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;
}

Upvotes: 4

Related Questions