JAX-RS JAXB JSON Response not working (MessageBodyProviderNotFoundException)

Have I ever worked on how to create a JAX Restful Service ... using the tutorial available here - Jersey

As explained in section 2.3.2, I added the following dependency in Maven -

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

In 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> 

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

When I try to access this via @localhost: 8080 / HMS_Web / services / ManagePatient / getPatient

I get

 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. 

and I also see a warning below in the logs -

 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. 

If Jersey 2.0 supports JAXB-based xml or json support, as mentioned in "8.1.1.2 JAXB-based JSON Support" in the Jersey manual, I'm not sure why I get provider errors.

Can any JAX-WS expert help me understand, as well as give me guidance on how to resolve this situation?

Thank you in advance

+4
source share
1 answer

you access the service through a browser, so your PatientController will try to display the response as html, I think this is the reason for

 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. 

try to run the service through the jersey client api as follows:

 WebTarget webTarget = client.target("http://localhost:8080/HMS_Web/services/ManagePatient/getPatient"); Patient patient = webTarget.request(MediaType.APPLICATION_XML_TYPE).get(Patient.class); 

for warning:

 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. 

I think you should remove:

 add(Patient.class); 

in MyApp . The patient is just POZH, he is neither a resource nor a supplier.

+2
source

Source: https://habr.com/ru/post/1495231/


All Articles