JAX-RS Overwrite method with same path

0

Currently I have a generic rest service, which receives all the calls and processes them according to the type of operation.

Some of the calls would be:

  • / user / 1 / v3 / people
  • / user / 1 / v3 / machinery

Then depending on whether they are post or get or even if they have a specific suffix, a list, insertion, search with filters, modification or elimination is called.

The thing is that you want for a specific operation, when you pass a POST without any suffix (in the superclass it would be an insert), you do a search with filters (because in the superclass would be with the suffix "/ search")

@Path("/user/{userId}{a:\/v3\/|\/}{operation}")
public class Service {

 @POST
 @Consumes(MediaType.APPLICATION_JSON)
 @Produces(MediaType.APPLICATION_JSON)
 public Response insert(@Context final HttpHeaders httpHeaders,         
 @PathParam(USER_ID) final Integer userId,
        @PathParam(OPERATION_LABEL) final String oper, final String record) {
     //......
 }

 @POST
 @Path("/search")
 @Consumes(MediaType.APPLICATION_JSON)
 @Produces(MediaType.APPLICATION_JSON)
 public Response search(@Context final UriInfo uriInfo, @Context final HttpHeaders httpHeaders,
        @PathParam(USER_ID) final Integer userId, @PathParam(OPERATION_LABEL) final String oper,
        final String record) {

     //...........
  }
}

I have created a new class that has first specified the type of operation ("treatments") and then I want to overwrite the insert class, so that instead of doing an insert, I do a search.

@Path("/user/{userId}{a:\/v3\/|\/}tratamientos")
public class TratamientosService extends Service {

 @POST
 @Consumes(MediaType.APPLICATION_JSON)
 @Produces(MediaType.APPLICATION_JSON)
 @Override
 protected Response insert(final HttpHeaders httpHeaders, final Integer userId, final String oper, final String record) {
     //..............
 }
}

But you are giving me an error that does not overwrite the method.

GRAVE: Servlet [appServlet] in web application [/vnwebservicesnew] threw load() exception org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization. [[FATAL] A resource model has ambiguous (sub-)resource method for HTTP method POST and input mime-types as defined by"@Consumes" and "@Produces" annotations at Java methods public javax.ws.rs.core.Response com.visualnacert.ws.version.base.services.TratamientosService.insert(javax.ws.rs.core.UriInfo,javax.ws.rs.core.HttpHeaders,java.lang.Integer,java.lang.String,java.lang.String) and public javax.ws.rs.core.Response com.visualnacert.ws.version.base.services.Service.insert(javax.ws.rs.core.HttpHeaders,java.lang.Integer,java.lang.String,java.lang.String) at matching regular expression /user/([^/]+)(\/v3\/|\/)tratamientos. These two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will always fail.; source='org.glassfish.jersey.server.model.RuntimeResource@18a82922']

Alternative path

I have also tried that the logic within the methods is in another protected method and in TratamientosService overwrite this protected method, but even though I do not skip error, when making the call, it does not go through the overwritten class.

Would there be any way that @Path("/user/{userId}{a:\/v3\/|\/}tratamientos") have higher priority than @Path("/user/{userId}{a:\/v3\/|\/}{operation}")

    
asked by Cadeq 27.11.2018 в 11:02
source

1 answer

2

The problem is that in both cases " tratamientos " is (potentially) part of the path, only that in one method you capture it as a value more than PathParam operation and in the other no.

The system can not know which method to apply to the path.

The simple solution, perhaps not the best, is to use only the method that captures {operation}, and make the call from the other method from there:

if(operation.equals("tratamientos")){
   return llamarMetodoDeTratamientos(userId, uriInfo, httpHeaders, oper, records);
}
    
answered by 27.11.2018 / 11:56
source