简体   繁体   English

具有多个参数的JAX-RS Post方法

[英]JAX-RS Post method with multiple parameters

I want to make a JAX-RS web service using jersey implementation. 我想使用泽西实现制作JAX-RS Web服务。 I need a post method with 3 parameters. 我需要一个带3个参数的post方法。

@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: 有一个单独的User类如下:

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). 首先,我可以使用jersey-media-moxy-2.3.jar文件转换为JSON(我不想使用maven)。 because this jar file is not converting content to json but if i use maven its working fine without parameters in post method. 因为这个jar文件没有将内容转换为json,但如果我使用maven,它的工作正常,没有post方法中的参数。

second, how to get param in body of method if i am using only one param. 第二,如果我只使用一个参数,如何在方法体中得到参数。 ie second method 即第二种方法

third, how to use multiple params in post method. 第三,如何在post方法中使用多个params。

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. 最后,我无法以json格式获取数据。

NOTE: I am making web service for android mobile. 注意:我正在为Android手机制作网络服务。 i am going to consume it via andorid. 我将通过andorid消费它。

for RESTful API, you should not be relay on the usual web application parameter passing style, 对于RESTful API,您不应该在通常的Web应用程序参数传递样式上进行中继,

... URL..?param=value

you should form a url in a way that, it make sense to access the resource: 你应该以一种方式形成一个url,访问资源是有意义的:

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. 这就是你可以在同一个Post请求中访问多个Path参数的方法。

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 另一方面,如果您正在讨论如何获取“User”类中所有字段的值,那么您应该考虑使用@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] : 现在,如果您使用POST方法发送如下所示:[JSON格式]:

{
    "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. jersey将使用请求正文中的数据创建User类的实例。 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. 这就是你在User类中需要强制空构造函数的原因,jersey将首先使用空构造函数创建类的实例,并调用每个字段的setter来设置值。

with that in place, if you simple put User in parameter of your method you will have object passed to your method. 有了这个,如果你简单地将User放在你的方法的参数中,你将把对象传递给你的方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM