简体   繁体   中英

How REST API receive request body data?

I am using an action in struts2 to post json to a REST API.

Now to post Jan object I do as following

  1. use JSONObject.fromObject(Object object).toString ,
  2. then use postmethod.setRequestEntity() ,
  3. finally client excute post method

So how should REST API receive data ?

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,
  2. then in action i convert user to xml,of course you should set 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

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. You can refer the following.

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.

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. 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..

  @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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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