1

I am running my REST service(Jersey) in Jetty.When I invoke REST method with XML media type in Response it works fine but when the media type is JSON the parent tag is missing from json response.Can someone help me how to resolve the issue?

Thanks in advance. The resource class is:

    public class Service {

        @Autowired
        private StudentDao studentDao;


        @Autowired
        private TutorDao tutorDaoImpl;

        @GET
        @Produces({MediaType.APPLICATION_JSON})
        @Path("/{tutorId}")
        public AllStudentSession getAllStudentForTutor(
                @PathParam("tutorId") final String tutorId) {
            return tutorDaoImpl.getAllAssignedStudent(tutorId);

        }
    }


@XmlRootElement(name = "all_student_session_info")
public class AllStudentSession {
    private List<StudentSessionsResp> allStudentAssigned;

    @XmlElement(name = "allStudentAssigned")
    public List<StudentSessionsResp> getAllStudentAssigned() {
        return allStudentAssigned;
    }

    public void setAllStudentAssigned(
            List<StudentSessionsResp> allStudentAssigned) {
        this.allStudentAssigned = allStudentAssigned;
    }

}

@XmlRootElement(name = "assigned_students_tutor")
public class StudentSessionsResp {
    private String childId;
    private String childName;
    private String parentName;
    private String subject;
    private String grade;
    private String phoneNumber;
    private AddressResp address;
    private String email;

    @XmlElement(name = "childId")
    public String getChildId() {
        return childId;
    }

    @XmlElement(name = "parentName")
    public String getParentName() {
        return parentName;
    }

    @XmlElement(name = "subject")
    public String getSubject() {
        return subject;
    }

    @XmlElement(name = "grade")
    public String getGrade() {
        return grade;
    }

    @XmlElement(name = "phoneNumber")
    public String getPhoneNumber() {
        return phoneNumber;
    }
    @XmlElement(name = "studentName")
    public String getChildName() {
        return childName;
    }
    @XmlElement(name = "address")
    public AddressResp getAddress() {
        return address;
    }
    @XmlElement(name = "email")
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setChildName(String childName) {
        this.childName = childName;
    }

    public void setAddress(AddressResp address) {
        this.address = address;
    }

    public void setChildId(String childId) {
        this.childId = childId;
    }
    public void setParentName(String parentName) {
        this.parentName = parentName;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
    public void setGrade(String grade) {
        this.grade = grade;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }


}

The desired output is:

{
  "all_student_session_info": {
    "allStudentAssigned": [
      {
        "address": {
          "city": "testcity",
          "country": "testcountry",
          "houseNumber": "10",
          "pinCode": "123456",
          "state": "teststate"
        },
        "childId": "55b009ab4f48f6da3a135bcc",
        "studentName": "Testname",
        "email": "testmail",
        "parentName": "testname",
        "phoneNumber": "123456789"
      },
      {
        "address": {
          "city": "testcity",
          "country": "testcountry",
          "houseNumber": "10",
          "pinCode": "123456",
          "state": "teststate"
        },
        "childId": "55b3e2114f482e535a109366",
        "studentName": "Testname",
        "email": "testmail",
        "parentName": "testname",
        "phoneNumber": "123456789"
      }
    ]
  }
}

The output I am getting is:

{
    "allStudentAssigned": [
      {
        "address": {
          "city": "testcity",
          "country": "testcountry",
          "houseNumber": "10",
          "pinCode": "123456",
          "state": "teststate"
        },
        "childId": "55b009ab4f48f6da3a135bcc",
        "studentName": "Testname",
        "email": "testmail",
        "parentName": "testname",
        "phoneNumber": "123456789"
      },
      {
        "address": {
          "city": "testcity",
          "country": "testcountry",
          "houseNumber": "10",
          "pinCode": "123456",
          "state": "teststate"
        },
        "childId": "55b3e2114f482e535a109366",
        "studentName": "Testname",
        "email": "testmail",
        "parentName": "testname",
        "phoneNumber": "123456789"
      }
    ]
  }

Here is the screen shot of the jar i am using: enter image description here

user3062513
  • 410
  • 1
  • 10
  • 19

1 Answers1

1

By default, Jackson does not wrap the JSON object with the root name, as you are expecting We can configure it to do so though. We can do that by configuring Jackson's ObjectMapper

ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
// maintain JAXB annotation support
mapper.registerModule(new JaxbAnnotationModule());

To configure the Jersey application to use this ObjectMapper, we can create a ContextResolver as seen here. Depending on how your app is set up to register resources and providers, either the ObjectMapperContextResolver will be automatically register by the @Provider annotation (if we are scanning packages), or you need to explicitly register it.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thanks for the help.I have implemented the provider and got it registered in spring.I am getting MIME media type application/json was not found. as error now.Can you please tell me what I am missing? – user3062513 Jul 26 '15 at 08:47
  • I would need to see your jersey configuration and also see the Jackson provider dependency you are using. In general, just adding the ContextResolver should not cause this type of problem – Paul Samsotha Jul 26 '15 at 08:53
  • Are you adding jar independently or are you using Maven? And I still need to see your Jersey configuration. – Paul Samsotha Jul 26 '15 at 09:05
  • 1
    Try and add the two from [here](http://stackoverflow.com/questions/30229285/jackson-messagebodywriter-not-found-when-returning-a-json-pojo-from-a-get-api/30229707#30229707) that you are missing, and remove the `jersey-json`. Then register the `JacksonJaxbJsonProvider`. You should find it in the `jackson-jaxrs-json-provider` jar. – Paul Samsotha Jul 26 '15 at 09:09
  • I have a Q.If I change the media type to application/x-www-form-urlencoded It complains MIME media type application/x-www-form-urlencoded was not found however i have added the mediatype to the method.Can you please what needs to be done in order to resolve it? – user3062513 Jul 30 '15 at 19:25
  • Pleas post about another question – Paul Samsotha Jul 31 '15 at 01:16