jax.ws.rs 中同一路径的多个函数?

Multiple functions for the same path in jax.ws.rs?

假设我想在我的应用程序上实现只读行为(不允许posts/puts)。我可以通过禁用这些 types/setting a @POST/@PUT 来捕获任何端点上的请求吗? (与在我的应用程序

中的每个 post/put 上放置一个布尔标志相反

您可以添加一个过滤器,禁止所有您不想支持的方法:

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.ext.Provider;

@Provider 
public class AuthorizationRequestFilter implements ContainerRequestFilter {
    private final List<String> disallowed=Arrays.asList("POST","PUT","DELETE","PATCH");

   @Override
   public void filter(ContainerRequestContext requestContext)
                throws IOException {
        if (disallowed.contains(requestContext.getMethod())){
            requestContext.abortWith(Response
                    .status(Response.Status.FORBIDDEN)
                    .entity("User cannot modify the resource.")
                    .build());
        }
    }
}