1 Reply Latest reply on Dec 6, 2015 6:38 AM by Joseph Hwang

    Json data is not wrapped from RESTEasy services

    Joseph Hwang Novice

      I implements test RESTEasy services. These are simple codes.

       

      ====Memeber.java

      @XmlAccessorType(XmlAccessType.NONE)

      @XmlRootElement(name = "member")

      public class Member implements Serializable{

       

      @XmlElement(name = "id")

      private String id;

       

      @XmlElement(name = "passwd")

      private String passwd;

       

      @XmlElement(name = "age")

      private int age;

       

      @XmlElement(name = "name")

      private String name;

       

      public String getId() {

        return id;

      }....

       

      == REST Service java

      @Path("/services")
      public class RESTEasyServices {


         @GET
         @Path("getJson/{id}")
         @Produces(MediaType.APPLICATION_JSON)
         public Response sayJsonHello(@PathParam("id") String id){
            Member m = new Member();
            m.setId(id);
            m.setPasswd("aaa");
            m.setAge(45);
            m.setName("joseph");
       
            return Response.status(200).entity(m).build();
         }

      }

       

      Invocation URI is successful. But Json data is not wrapped.

      {"id":"aupres","passwd":"aaa","age":45,"name":"joseph"}

      However when i set the attribute of @Produce to MediaType.APPLICATION_XML like below,

      @Produces(MediaType.APPLICATION_XML)

      response of XML data is wrapped.

       

      <member>
      <id>aupres</id>
      <passwd>aaa</passwd>
      <age>45</age>
      <name>joseph</name>
      </member>
      I have no idea how to wrap the json data.
      Your help will be deeply appreciated. Thank in advanced!
        • 1. Re: Json data is not wrapped from RESTEasy services
          Joseph Hwang Novice

          I solved it. These are my codes

           

          @GET

             @Path("getJson/{id}")

             @Produces(MediaType.APPLICATION_JSON)

             public Map<String, List<Member>> sayJsonHello(@PathParam("id") String id){

                Member m1 = new Member();

                m1.setId(id);

                m1.setPasswd("aaa");

                m1.setAge(45);

                m1.setName("joseph");

               

                Member m2 = new Member();

                m2.setId("hwa5383");

                m2.setPasswd("bbb");

                m2.setAge(40);

                m2.setName("jina");

               

                List<Member> list = new ArrayList();

                list.add(m1);

                list.add(m2);

               

                Map<String, List<Member>> mems = new HashMap();

                mems.put("member", list);

                      

                return mems;

             }

           

          Then the output is Json type

           

          {"member":[{"id":"aupres","passwd":"aaa","age":45,"name":"joseph"},{"id":"hwa5383","passwd":"bbb","age":40,"name":"jina"}]}

           

          Thanks any way.