从接口设置Jersey响应状态代码,而不返回响应 [英] Setting a Jersey response status code from interface without returning Response

查看:125
本文介绍了从接口设置Jersey响应状态代码,而不返回响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置以下Jersey REST端点的响应状态

I am trying to set a response status of the following Jersey REST endpoint

@Path("/roles")
public interface IRoleService {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    Role create(Role role);

}

由于它创建了新资源,因此如果返回状态码201,但当前返回200,则是合适的.

Since it creates a new resource it would be appropriate if it returned Status code 201 but currently it returns 200.

我发现如何设置状态码的唯一方法是让方法返回javax.ws.rs.core.Response并将其设置在那里,但是我真的不希望我的所有接口都返回通用Response而不是实际的Response对象. (在这种情况下为Role).

The only way I found how to set the status code is to have the method return a javax.ws.rs.core.Response and set it there, but I really do not want all of my interfaces to return a generic Response instead of the actual response object (in this case Role).

推荐答案

一种方法是创建自定义注释,并使用响应过滤器设置状态.例如

One way would be to create a custom annotation and using a response filter to set the status. For example

注释

@NameBinding
@Target({METHOD, TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Status {
    int DEFAULT_CODE = 0;

    int code() default DEFAULT_CODE;
}

ContainerResponseFilter

@Status
@Provider
public class StatusFilter implements ContainerResponseFilter {

    @Context
    private ResourceInfo info;

    @Override
    public void filter(ContainerRequestContext req, ContainerResponseContext res) throws IOException {
        Status status = getInterfaceAnnotation(info.getResourceMethod());
        if (status != null) {
            int code = status.code();
            if (code != Status.DEFAULT_CODE && res.getStatus() == 200) {
                res.setStatus(code);
            }
        }
    }

    private static Status getInterfaceAnnotation(Method resourceMethod) {
        String methodName = resourceMethod.getName();
        Class<?>[] paramTypes = resourceMethod.getParameterTypes();
        Class<?> iface = resourceMethod.getDeclaringClass().getInterfaces()[0];
        Method ifaceMethod;
        try {
            ifaceMethod = iface.getDeclaredMethod(methodName, paramTypes);
        } catch (NoSuchMethodException e) {
            return null;
        }
        return ifaceMethod.getAnnotation(Status.class);
    }
}

在过滤器中,我们使用ResourceInfo获取方法,并进行一些反射以获得@Status批注.从那里,可以获取状态代码并在响应上进行设置.

In the filter, we get the method with ResourceInfo and do some reflection to get the @Status annotation. The from there, can grab the status code and set it on the response.

由于它是一个名称绑定"过滤器,因此只会为使用它进行注释的方法调用.在此处.

Since it is a Name Binding filter, it will only be called for methods that are annotated with it. See more here.

然后使用它,只需将注释添加到方法中即可.

Then to use it, just add the annotation to the method.

public interface ITestResource {
    @GET
    @Status(code=201)
    String get();
}

如果您需要添加一些自定义标头,则可以对标头执行相同的操作.

The same could be done for headers if you need to add some custom headers.

这篇关于从接口设置Jersey响应状态代码,而不返回响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆