1

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.

asked Apr 4, 2016 at 6:21
2

1 Answer 1

4

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;
}
answered Apr 4, 2016 at 20:50
Sign up to request clarification or add additional context in comments.

4 Comments

how to pass two parameters in a single method? and the first example that you gave, how can i pass param in url in post method? i guess it is wrong if i pass username and password kind of thing in url.
@Sarvesh - of course you should NOT add password as your REST URI path parameter. that doesn't make sense. That is why I took a different example. If you want to pass data in the body you can use the second example. Your question was not specific enough, so I gave both answers.
can i have multiple param in post method ? Also in order to convert java to json, maven provide a dependency of jarsey-media-moxy, but if i am adding jar file of it, its not working
yes you can pass multiple parameters. look at how I re-write your User, that is for jersy to automatically convert the input in POST method body to a pojo. jersy does both way conversion. java object to JSON/XML or vise versa.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.