0

We would usually define POST and PUT verbs as different service APIs.

@POST
@Path("/getbook")
@Produces({"application/xml","application/json"})
@Consumes({"application/xml","application/json","application/x-www-form-urlencoded"})
public Response getBucket() {
...        } 
@PUT
@Path("/getbook/{name}")
@Produces({"application/xml","application/json"})
@Consumes({"application/xml","application/json","application/x-www-form-urlencoded"})
public Response getBucket(@PathParam("name") String name) {
...        }

Would there be a way to combine these verbs into a single method - and then drive different logic based on the type of the verb ? Hypothetically

@POST 
@PUT
@Path("/getbook/{name}")
@Produces({"application/xml","application/json"})
@Consumes({"application/xml","application/json","application/x-www-form-urlencoded"})
public Response getBucket(@PathParam("name") String name) {

if(verb=POST){
...        }
else{

}
}
4

1 回答 1

0

您可以尝试使用MessageContext. 您需要将上下文注入到服务方法中,如下所示的updateCustomer方法,然后您可以根据需要检查方法类型(这里我正在检查 PUT):

@Path("/customer")
public class CustomerService {

    @Context
    private org.apache.cxf.jaxrs.ext.MessageContext mc; 

    @PUT
    public Response updateCustomer(@Context MessageContext context, Customer c) {
        HttpServletRequest request = context.getHttpServletRequest();

        boolean isPut = "PUT".equals(request.getMethod());
    }
}
于 2015-07-13T09:47:52.087 回答