JAX-RS exception: Annotated with GET of resource, class is not recognized as valid resource method

java, jax-rs, rest, service, web-services

Solution

JAX-RS cannot automatically convert a @PathParam (which is a string value), into an `Employee` object. Requirements for objects that can be automatically created from a @PathParam are:

- String (defacto, because the data is already a string)

- Objects with a constructor that accepts a (single) string as argument

- Objects with a static `valueOf(String)` method

For cases 2 & 3 the object would be required to parse the string data and populate its internal state. This is not normally done (because it forces you to make assumptions about the content type of the data). For your situation (just beginning to learn JAX-RS), its best to just accept the incoming @PathParam data as a String (or Integer, or Long).

@GET
@Path("/emp/{id}")
public Response getEmpDetails(@PathParam("id") String empId) {
    return Response.status(200).entity(empId).build();
}

Passing a complex object representation to a REST service in a GET method doesn't make much sense, unless its being used, eg, as a search filter. Based on your feedback, that is what you are trying to do. I've actually done this on a project before (generic implementation of search filters), the one caveat being that you need to strictly define the format of the search data. So, lets define JSON as the accepted format (you can adapt the example to other formats as needed). The 'search object' will be passed to the service as a query parameter called `filter`.

@GET
@Path("/emp")
public Response getEmployees(@QueryParam("filter") String filter) {
    // The filter needs to be converted to an Employee object. Use your
    // favorite JSON library to convert. I will illustrate the conversion
    // with Jackson, since it ships with Jersey
    final Employee empTemplate = new ObjectMapper().readValue(filter, Employee.class);

    // Do your database search, etc, etc
    final String id = getMatchingId(empTemplate);

    // return an appropriate response
    return Response.status(200).entity(id).build();
}

And in your client class:

final String json = new ObjectMapper().writeValueAsString(emp);
service
    .path("rest")
    .path("emp")
    .queryParam("filter", json)
    .accept(emp, MediaType.TEXT_PLAIN)
    .get(String.class)

Problem

I am using the jersey implementation of JAX-RS for the web service. I am very new to this JAX-RS. I am trying to add a method in the service which accept an Employee object and returns the employee Id based on the Employee object values (there is a DB hit for this). Following the Restful principles, I made the method as @GET and provided the url path as shown below: ``` @Path("/EmployeeDetails") public class EmployeeService { @GET @Path("/emp/{param}") public Response getEmpDetails(@PathParam("param") Employee empDetails) { //Get the employee details, get the db values and return the Employee Id. return Response.status(200).entity("returnEmployeeId").build(); } } ``` For testing purpose, I wrote this Client: ``` public class ServiceClient { public static void main(String[] args) { ClientConfig config = new DefaultClientConfig(); Client client = Client.create(config); WebResource service = client.resource(getBaseURI()); Employee emp = new Employee(); emp.name = "Junk Name"; emp.age = "20"; System.out.println(service.path("rest").path("emp/" + emp).accept(MediaType.TEXT_PLAIN).get(String.class)); } private static URI getBaseURI() { return UriBuilder.fromUri("http://localhost:8045/AppName").build(); } ``` } When I run it, I am getting the error: `Method, public javax.ws.rs.core.Response com.rest.EmployeeService.getEmpDetails(com.model.Employee), annotated with GET of resource, class com.rest.EmployeeService, is not recognized as valid resource method.` Edit: Model: ``` package com.model; public class Employee { public String name; public String age; } ``` Please let me know where is the issue, I am a beginner in this and struggling to understand the concepts :(

Original source

Related problems