Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

After overriding the Application.getClasses() by a custom MessageBodyReader, methods on resource classes cannot be invoked

In a RESTEasy project running on Wildfly server, there is a resource class:

@Path("/company")
public class CompanyResource {
  @Inject
  CompanyService companyService;

  @PUT
  @Consumes(MediaType.APPLICATION_JSON)
  public void update(Company company) {
    companyService.update(company);
  }
}

Initially the REST API configuration class just extends Application without any extra @override on the existing methods of Application class. An http request, http://localhost:8080/workcontext/company, with PUT as the http request method could work, meaning the CompanyResource.update() can be invoked successfully when receiving the aforementioned http request.

However, I then tried to add a custom MessageBodyReader<Company>:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class CompanyReader implements MessageBodyReader<Company> {

  @Override
  public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
    return true;
}

  @Override
  public Company readFrom(Class<Company> type, Type genericType, Annotation[] annotations, MediaType mediaType,
        MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
        throws IOException, WebApplicationException {
    try(JsonReader reader = Json.createReader(entityStream)) {
        JsonObject companyJson = reader.readObject();
        Company company = new Company();
        company.setCompanyCode(companyJson.getString("companyCode"));
        company.setName(companyJson.getString("name"));
        company.setHeadquarter(Region.valueOf(companyJson.getString("headquarter")));
        return company;
    }
  }

}

In order to make this custom MessageBodyReader<Company> work, I registered this class by overriding the Application.getClasses():

public class JaxRsConfiguration extends Application {
  @Override
  public Set<Class<?>> getClasses() {
      Set<Class<?>> classes = new HashSet<>();
      classes.add(CompanyReader.class);
      return classes;
  }
}

I expected that this MessageBodyReader<Company> could be invoked when sending the same http PUT request, but on the opposite the response is: RESTEASY003210: Could not find resource for full path: http://localhost:8080/workcontext/company

Question: How to make this custom MessageBodyReader work?

>Solution :

You should annotate you’re CompanyReader with @Provider. In your application if you return any classes in Application.getClasses() or Application.getSingletons() then, per the spec, those are the only classes allowed to be used in your application.

If either getClasses or getSingletons returns a non-empty collection then only those classes or singletons returned MUST be included in the published JAX-RS application.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading