In this short tutorial we will learn how to fix a common error that you can hit when deploying REST Web service that produce or consume JSON content type.
Error description:
RESTEASY004655: Unable to invoke request: jakarta.ws.rs.ProcessingException: RESTEASY003215: could not find writer for content-type application/json type:
A common cause of this issue is that you are either:
- Missing a JSON Provider such as Jackson2. Jackson is is a multi-purpose Java library for processing JSON data format.
- An incorrect version of Resteasy Client
Example of the correct dependencies for a RESTeasy Endpoint that produces/consumes JSON Media type:
<dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-client</artifactId> <version>6.2.0.Final</version> </dependency> <dependency> <groupId>org.jboss.logging</groupId> <artifactId>commons-logging-jboss-logging</artifactId> <version>1.0.0.Final</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jackson2-provider</artifactId> <version>6.2.0.Final</version> </dependency>
Sample JAX-RS Application that consumes/produces JSON media type
Here is a minimal REST Endpoint you can use as an example:
@Path("/service") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public class ParamRESTService { @Inject DataList ejb; @GET @Path("/{id}") public SimpleProperty getPropertyById(@PathParam("id")int id) { return ejb.getList().get(id); } @GET public List<SimpleProperty> getProperty() { return ejb.getList(); } @POST public Response createProperty( SimpleProperty p) { int n = ejb.addToList(p); return Response.status(Response.Status.OK).build(); } }
The DataList is an EJB that stores an ArrayList of a Java POJO in memory:
@ApplicationScoped @Named public class DataList implements Serializable { private List<SimpleProperty> list; @PostConstruct public void init() { list = new ArrayList<SimpleProperty>(); } public List<SimpleProperty> getList() { return list; } public void setList(List list) { this.list = list; } public int addToList(String key, String value) { list.add(new SimpleProperty(key,value)); return list.size(); } public int addToList(SimpleProperty p) { list.add(p); return list.size(); } }
Testing the REST Service
In order to Test the REST Service you can use the following JUnit Test Class:
public class TestClient { String BASE_URL ="http://localhost:8080/ee-rest-client/rest"; @Test public void testParam() { Client client = ClientBuilder.newClient(); SimpleProperty p1 = new SimpleProperty("mykey","value"); WebTarget myResource = client.target(BASE_URL).path("/service"); Response rs = myResource.request(MediaType.APPLICATION_JSON) .post(Entity.json(p1), Response.class); assertEquals(rs.getStatus(),200); SimpleProperty property = client.target(BASE_URL).path("/service/{key}") .resolveTemplate("key", "0") .request(MediaType.APPLICATION_JSON) .get(SimpleProperty.class); assertNotNull(property); List<SimpleProperty> result = client.target(BASE_URL).path("/service") .request(MediaType.APPLICATION_JSON).get(new GenericType<List<SimpleProperty>>() { }); assertEquals("mykey",result.get(0).getKey()); assertEquals("value",result.get(0).getValue()); } }
Conclusion
This article was a quick howto to help you with the error βRESTEASY003215: could not find writer for content-type application/json type:β.