Raju Raju - 2 months ago 10
JSON Question

MessageBodyWriter not found for media type=application/json ,added Jakson dependency still issue persisit

I am creating restful webservice ,I am trying to get json response but getting Message writer not found exception.In Jersey site it was mentioned to use Jakson dependency for application/JSON so i used it still getting the same error. For response type application/xml it is working fine.
These are dependency available in POM ,still getting Message writer not found exception.Can anyone please help -

<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.2</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>2.2</version>
</dependency>
public class UserServiceTest extends JerseyTest {

@Override
protected Application configure(){
ResourceConfig resourceConfig=new ResourceConfig(UserService.class);
return resourceConfig;
}
@Test
public void test(){
String output=target("/users/test").request().get(String.class);
System.out.println(output);
//assertEquals("abc",output);
Assert.assertEquals("abc",output);
}


}

Answer

Prior to Jersey 2.9, you need to register the JacksonFeature (where after, it is automatically registered). In your case just register it with the ResourceConfig

resourceConfig.register(JacksonFeature.class)

The ResourceConfig is only for configuring the server side. For the client side, you can register the feature like

@Override
public void configureClient(ClientConfig config) {
    config.register(JacksonFeature.class)l
}

in your JerseyTest.