12
votes

How can the following produce this error when they have different URLs ?

@Path("/job/{empId}/empProfile")
public EmpProfileResource delegateToEventProfileResource() {
    EmpProfileResource resource = new EmpProfileResource();
    locator.inject(resource);
    return resource;
}

@Path("/job/{empId}/empTask")
public EmpTaskResource getClientLevelAttendees(@PathParam("clientId") long clientId){
    EmpTaskResource resource = new EmpTaskResource (empId);
    locator.inject(resource);
    return resource;
}


@Path("/")
public class EmpTaskResource{
}
@Path("/")
public class EmpProfileResource{
}

Yes, they both are GET and produce the same, so ?

@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })

Error:

[[FATAL] A resource model has ambiguous (sub-)resource method for HTTP method GET and input mime-types as defined by"@Consumes" and "@Produces" annotations at Java methods public javax.ws.rs.core.Response com.EmpTaskResource.getEmpTasks(java.time.LocalDate,java.time.LocalDate,java.lang.String) and public javax.ws.rs.core.Response com.EmpProfileResource.getEmpProfiles(long,java.lang.String) at matching regular expression /. These two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will always fail.;

4
Be careful that you dont have two endpoints that resolve to the same path using the same HTTP verb. I had two endpoints, both using POST and I got this error.Paul Rooney

4 Answers

10
votes

Remove the @Path("/") from the sub-resource classes. Sub-resource classes don't need them. And if they have them, they get added as root resource classes, if you are scanning for @Path annotated classes. And this is the problem. You haven't show the methods of the sub-resource classes, but because the have the same root path, I would imagine that the problem is caused by some overlapping methods. So just remove the @Path("/") on sub-resource classes, and you should be OK.

3
votes

I had the same error. Most people resolved the error by changing their @Path annotations because they where ubiquitous. In my case something different happened. I modified a package from aaa to bbb for example. For some reason in the artifact deployed to the server there where both the aaa and bbb packages, so the resources where duplicated and the server raised the exception. I had to clear the previous deployed artifact and deploy the new. Someone may check this case also where the error appears. Of course at the end of the day the reason is again a path ubiquity. I am suffering with the same problem i have also updated a version check but its give me a same problem may jersey frame works is not supporting this

0
votes

This error arises if we have written @PathParam instead of @Path in our service class or where ever we are defining the path for same.

0
votes

I had this error two times now. Both were different:

First time I had at the following above GET and POST Methods etc.

@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})

But I didn't have it infront of the class. So if you have different class-ressources like:

@Path("/users") 
public Class User{}

and

@Path("/lists")
public Class List{}

Then BOTH of them need the

@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})

infront of them:

@Path("/users")
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON}) 
public Class User{}

Second time I moved a resource (endpoint) class from one folder to a different one (via refactor from Intellij) and apparently the IDE doesn't recognize this. Then the exact problem occurs which Panagiotis Piperopoulos already described.

To fix it I did the following: Go to the folder where you project is located (<working_directory>). There you will find a folder called target (<working_directory>\target)

There was in my case 1: a war file. Delete that.

2: A folder which contains the meta-inf and web-inf and index.html. The name is probably your module name (in IntelliJ: File -> Project Structure -> Module). Rename that folder to something like <old_name>_old

If you re-compile your code then the folder and a new .war file should appear.