简体   繁体   English

REST API如何接收请求正文数据?

[英]How REST API receive request body data?

I am using an action in struts2 to post json to a REST API. 我在struts2中使用操作将json发布到REST API。

Now to post Jan object I do as following 现在发布Jan对象,如下

  1. use JSONObject.fromObject(Object object).toString , 使用JSONObject.fromObject(Object object).toString
  2. then use postmethod.setRequestEntity() , 然后使用postmethod.setRequestEntity()
  3. finally client excute post method 最终客户执行帖子的方法

So how should REST API receive data ? 那么REST API应该如何接收数据?

Here is code a segment : 这是一段代码:

@POST
    @Path("addUser")
    @Produces("text/plain")
    @Consumes(MediaType.APPLICATION_JSON)
    public String addUser() {

    }; 
  1. First I add @XmlRootElement(name="user") to my model--user, 首先,我将@XmlRootElement(name="user")到我的模型中-用户,
  2. then in action i convert user to xml,of course you should set Content-Type", MediaType.APPLICATION_ATOM_XMl 然后将用户转换为xml,当然,您应该设置Content-Type", MediaType.APPLICATION_ATOM_XMl

  3.  @POST @Path("addUser") @Produces("text/plain") @Consumes(MediaType.APPLICATION_ATOM_XML) public String addUser(User user) {} 
  4. add

     <init-param> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> <param-value>true</param-value> </init-param> 

to web.xml 到web.xml

finally you can get user. 最终您可以获得用户。

If I understand your question as I think, to receive a JSON String in REST API, you can use a JAXB. 如果我理解您的问题,要在REST API中接收JSON字符串,可以使用JAXB。 You can refer the following. 您可以参考以下内容。

REST API REST API

@POST
@Path("addUser")
@Produces("text/plain")
@Consumes(MediaType.APPLICATION_JSON)
public String addUser(Student s) {
    //Your logic here
    return "user added";
}; 

JAXB representation for student. 学生的JAXB表示形式。

public class Student {
    String id;

    String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    String age;

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public Student(String id, String name, String age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Student() {

    }
}

When you post Student JSON String, you will get the Raw Student object in addUser method. 发布学生JSON字符串时,您将在addUser方法中获得Raw Student对象。 Correct me, if my understanding is wrong. 如果我的理解是错误的,请纠正我。

If you want to access the body part in back-end code using Rest API (JAX-WS-RS) with jersey and apache-cxf then you need to configure your rest package of classes like that.. 如果要使用带有jersey和apache-cxf的Rest API(JAX-WS-RS)在后端代码中访问正文部分,则需要配置此类的rest包。

  @Path("/student")  //path of rest package of class 
  @Consumes("application/json")  //If you want to consumed produced json
  @Produces("application/json") //If you want to produced json
  public class StudentRest{

     Student student=new Student();

     @GET
     @Path("returnstudent")
     public Student ReturnStudentMethod()
     {
            return student;
     }

     //if you want to receive or produced some specific type then write
     @GET
     @Produced("application/pdf")
     @Path("returnpdffile")
     public Response ReturnPdfFile()
     {
            return file;
     }

} }

And also you need to set web.xml if you are using jaxrs with jersey
   <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
   </init-param>

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

相关问题 如何将请求正文中的列表值发送到 Rest Api - How to send List value in Request Body to Rest Api 如何在springboot(REST)的ExceptionHandler类中获取请求体数据 - How to get the request body data inside ExceptionHandler class in springboot(REST) 如何在 Spring 引导中通过 REST API 使用请求正文(JSON)以及多个文件(多部分/表单数据)? - How to consume Request Body (JSON) along with multiple files (multipart/form-data) via REST API in Spring Boot? 处理动态请求正文 - java rest api - Handle dynamic request body - java rest api 使用REST API从Web服务接收数据 - Receive data from a webservice using REST API 如何在REST API中的同一POJO中解析请求参数(查询和路径参数)和请求正文 - How to parse request parameter (query and path param) and request body in same POJO in REST API 在 Postman 的请求正文中传递多个 JSON 数据并使用 Jersy(JXRS) 进入 Java Rest API - Pass multiple JSON data in Request Body of Postman and Get into Java Rest API using Jersy(JXRS) 如何接收请求正文数据以及多部分图像文件? - How to receive request body data along with multi-part image file? 如何放心地在请求中发送复杂的正文? - How to send complex body in the request with rest assured? rest中如何写数组请求体放心 - How to write array request body in rest assured
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM