简体   繁体   中英

RESTEasy - Best practices to override service method to implement some generic logic across the different methods (like in servlet)

Ex: I have some common logic across different resources

@Path("/rest")
public class AddUser {

   @GET
   @Path("/AddUser/{ext}/{userId}")
   @Produces(MediaType.APPLICATION_JSON)
    public String addUser(@PathParam("tenantId") String tenantId, @PathParam("userId") Integer userId) {

   //I have some common logic here

}


@Path("/newrest")
public class AddUser1 {   
    @GET
    @Path("/AddUser/{ext}/{userId}")
    @Produces(MediaType.APPLICATION_JSON)
        public String addDifferentUser(@PathParam("tenantId") String tenantId,              @PathParam("userId") Integer userId) {

      //I have same common logic here as well
  }

} 

Which class i can extend to overwrite common logic for bot the rest services?

Overriding Service method is not recommended Should I override service ?

Take a look @ ContainerRequestFilter An extension interface implemented by container request filters. You can handle your common logic here.

You should probably not have bussiness logic directly in the communication layer. Imagine adding soap interface using different technology it will be imposible to use any common entry point.

You'd rather write service class and call it wherever you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM