0

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() {

    }; 
Andrea Ligios
  • 46,329
  • 24
  • 102
  • 208
Clarence
  • 11
  • 1
  • 3
  • Is it duplicated with http://stackoverflow.com/questions/18660487/consume-application-json-in-rest-services-using-jersey-in-osgi ?! – Alireza Fattahi Jun 07 '15 at 04:57
  • @AlirezaFattahi No, this question is about Struts 2. – Dave Newton Jun 07 '15 at 12:40
  • I think this guy have some problem receiving data, and he is using https://jersey.java.net to consume the rest api generate data. @Clarence please send more details – Alireza Fattahi Jun 08 '15 at 02:54
  • the question is if i send a Object(like User) not parameter(like name ) to rest api ,how should i get user . I don't know clearly where the problem is.But it is probably the data type,meidatype...but finally i solved...thank you ,anyway – Clarence Jun 08 '15 at 08:10
  • possible duplicate of [How to access parameters in a RESTful POST method](http://stackoverflow.com/questions/8194408/how-to-access-parameters-in-a-restful-post-method) – Roman C Jun 08 '15 at 09:16

3 Answers3

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

Andrea Ligios
  • 46,329
  • 24
  • 102
  • 208
Clarence
  • 11
  • 1
  • 3
0

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.

  • No,you are right.Actually the code i write is the same as you.but i can not receive data(user object) from client.so i found a new way and i did it. Then i would put my new way and thanks a lot – Clarence Jun 08 '15 at 07:25
0

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>
Pankaj Verma
  • 29
  • 1
  • 7