JAX-RS JAXB JSON响应无法正常工作(MessageBodyProviderNotFoundException)

我曾经工作过一段时间,想弄清楚如何创建一个JAX Restful Service …使用这里提供的指南 –
Jersey

如第2.3.2节所述,我在Maven中添加了以下依赖项 –

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.0</version>
</dependency>

在web.xml中

<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>
    </init-param>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.hms.rs.controller.MyApp</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>

和MyApp.java

public class MyApp extends Application{

     @Override
        public Set<Class<?>> getClasses() {
            return new HashSet<Class<?>>() {{
                // Add your resources.
            System.out.println("From the Myapp...");
                add(Patient.class);
                add(PatientController.class);

            // Add LoggingFilter.
                add(LoggingFilter.class);
            }};
        }
}

Patient.java –

@XmlRootElement(name = "Patient")
public class Patient {

    private String patientFName;
    private String patientLName;
    private int patientAge;
    private String patientSex;
    private String patientParentSpouse;
    private String patientQual;
    private String patientOccupation;
    private String patientComments;

    public Patient()
    {
    }

Setters and Getters....

}

PatientController.java –

@Path("/ManagePatient")
public class PatientController {
      @GET
  @Path("/getPatient")
  @Produces(MediaType.APPLICATION_XML)
  public Patient printPatient() {
      System.out.println("Hello.... from the PatientController");
      Patient ptnt = new Patient();
      ptnt.setPatientFName("FirstN");
      ptnt.setPatientLName("LName");
      ptnt.setPatientAge(30);
      ptnt.setPatientSex("M");
      ptnt.setPatientParentSpouse("ParentSpuse");
      ptnt.setPatientQual("engg");
      ptnt.setPatientOccupation("software");
      ptnt.setPatientComments("comments here");

      System.out.println("Patient = " + ptnt);

    //return ptnt.toString();
      return ptnt;
  } 

当我尝试通过浏览器访问@ localhost:8080 / HMS_Web / services / ManagePatient / getPatient

我正进入(状态

javax.servlet.ServletException: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/html, type=class com.hms.app.ui.beans.Patient, genericType=class com.hms.app.ui.beans.Patient.

我也在日志中看到以下警告 –

WARNING: A provider com.hms.app.ui.beans.Patient registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider com.hms.app.ui.beans.Patient will be ignored. 

如果Jersey 2.0支持基于JAXB的xml或json支持,如Jersey指南中提到的@ 8.1.1.2.基于JAXB的JSON支持,我不知道为什么我收到Provider错误.

任何JAX-WS专家都可以帮助我理解并指导我如何解决这种情况吗?

先感谢您

最佳答案 您正在通过浏览器访问该服务,因此您的PatientController将尝试将响应呈现为html,我想这就是为什么

javax.servlet.ServletException: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/html, type=class com.hms.app.ui.beans.Patient, genericType=class com.hms.app.ui.beans.Patient.

尝试通过jersey客户端api来提供服务,如下所示:

WebTarget webTarget = client.target("http://localhost:8080/HMS_Web/services/ManagePatient/getPatient");

Patient patient = webTarget.request(MediaType.APPLICATION_XML_TYPE).get(Patient.class);

警告:

WARNING: A provider com.hms.app.ui.beans.Patient registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider com.hms.app.ui.beans.Patient will be ignored. 

我想你应该删除:

add(Patient.class);

在你的MyApp中.患者只是一个POJO,既不是资源也不是提供者.

点赞