Jersey method selection by query parameters

后端 未结 3 1289
渐次进展
渐次进展 2021-01-23 03:14

I need to implement a webservice that uses the first query parameter to identify the operation, i.e. the client call would be something like: http://localhost:8080/ws/oper

3条回答
  •  我在风中等你
    2021-01-23 04:00

    I see this is already answered, but I had a similar problem and achieved a solution using sub-resources.

    package com.example.ws;
    
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.QueryParam;
    
    @Path("/")
    public class Operation {
    
        @Path("/")
        public Object sub(@QueryParam("operation") String operation)
        {
           if ("info".equals(operation))
              return new InfoOp();
           if ("create".equals(operation))
              return new CreateOp();
           //handle via error code
        }
    
        public class InfoOp {
            @GET 
            public String info() {
                return "info";
            }
        }
    
        public class CreateOp {
            @GET 
            public String create(@QueryParam("name") String name) {
                return "create: " + name;
            }
        }
    }
    

提交回复
热议问题