我已工作了一段時間弄清楚如何創建一個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高手幫我瞭解,也爲我提供瞭如何解決這個問題的方向?
預先感謝您
只需添加Genson到類路徑中,一切都應該工作。它也會自動支持jaxb註釋。 – eugen