2013-08-04 61 views
4

我已工作了一段時間弄清楚如何創建一個JAX RESTful服務......利用現有蒞臨指導 - JerseyJAX-RS JAXB JSON響應不工作(MessageBodyProviderNotFoundException)

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; 
    } 

當我試着通過瀏覽器@本地訪問此:8080/HMS_Web /服務/ 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. 

如果新澤西州2.0支持提到@節「8.1.1.2基於JAXB XML或JSON支持下面的警告。 JAXB基於JSON支持」澤西指南中,我不知道爲什麼我收到了供應商的錯誤。

可以在任何JAX-WS高手幫我瞭解,也爲我提供瞭如何解決這個問題的方向?

預先感謝您

+0

只需添加Genson到類路徑中,一切都應該工作。它也會自動支持jaxb註釋。 – eugen

回答

2

您訪問通過瀏覽器的服務,讓您的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. 

嘗試的理由comsume通過球衣服務客戶端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,它既不是資源也不是提供者。

+0

非常感謝你。我措辭的問題錯相對於使用瀏覽器,我使用捲曲檢索XML或JSON ......雖然檢索XML - 我得到的HTTP響應500(這我試圖找出原因),並在檢索JSON - 我越來越沒有找到媒體類型=應用程序/ JSON MessageBodyWriter。致力於爲此添加moxy。非常感謝你的迴應。 – Satya