How to send an answer as JSON in Jersey Vacation

I have a Jersey Rest Webservice web service with server side code like

@GET @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public List<Employee> getEmployees() { return new EmployeeService().getEmployees(); } 

T I am testing a service from a chrome rest client. This works fine with Accept: application / xml ** But when I request this with Accept code: application / json

I get the following exception: -

javax.servlet.ServletException:

 org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class com.rshekhar.domain.Employee, genericType=class com.rshekhar.domain.Employee. 

What is the right way to do this.

0
source share
3 answers

You can use Jersey's built-in Json JSON library to automatically serialize JSON from any POJO classes. You just need to add the Jackson JSON library to your pom.xml:

  <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>${jersey.version}</version> </dependency> 

Also, in order to activate the POJO Jersey display, you need to add the following lines to web.xml:

  <init-param> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> <param-value>true</param-value> </init-param> 

Then, if your class method is EmployeeService().getEmployees() returns a list of employees, Jersey will serialize it to something like (JSON array)

 {"employees": ["name":"Adam", "name":"Robert", ....]} 
+1
source

Can you provide more details on setting up a jersey (annotation of feature classes for beginners)

Are you using JAXB? What version of jersey?

Here is a good starting point for your goal: https://github.com/jasonray/jersey-starterkit/wiki/Serializing-a-POJO-to-xml-or-json-using-JAXB

0
source

Take a look here and try an example. This is almost the same as you mentioned.

several types of content in jax-rs

0
source

All Articles