如何为映射的HttpMethods Spring MVC @RestControllers设置默认成功状态代码?

时间:2015-04-02 10:54:32

标签: spring spring-mvc controller http-status-codes

你好春天大师,

目前,我们正在使用它自己的成功@ResponseStatus来注释每个映射的@RestController方法。我想知道,如果有可能进行全局设置,则基于映射的@RequestMapping方法。关于成功,我们希望:

  • GET - > HttpStatus.OK
  • POST - > HttpStatus.CREATED
  • PUT - > HttpStatus.OK
  • PATCH - > HttpStatus.OK
  • DELETE - > HttpStatus.NO_CONTENT

如果可能的话,我们该怎么做?

祝你好运, 的Marius

3 个答案:

答案 0 :(得分:2)

您可以使用Filter

来完成
public class ResponseStatusConverterFilter implements javax.servlet.Filter {

@Override
public void doFilter(ServletRequest req, ServletResponse res,
        FilterChain chain) throws IOException, ServletException {

    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;

    chain.doFilter(request, response);

    switch (request.getMethod()) {
        case "GET": response.setStatus(200);
                    break;
        case "POST": response.setStatus(201);
                     break;
        ...
    }
}

并在web.xml

中添加过滤器
<filter>
    <filter-name>responseStatusConverterFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>responseStatusConverterFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

请小心处理错误,因为这可能会覆盖错误响应代码。您应该在覆盖之前检查响应代码(例如,检查它是否有一些不良代码,如500或404,然后不要将其设置为200或201)。

另一种解决方案:

创建扩展HandlerInterceptorAdapter覆盖

的bean
void postHandle(HttpServletRequest request,
                HttpServletResponse response,
                Object handler,
                ModelAndView modelAndView)
                throws Exception

并添加类似于我的第一个解决方案的逻辑。 将bean定义添加到:

<mvc:interceptors>
   <bean class="com.your.web.ResponseConverterInterceptor" />
</mvc:interceptors>

附录:您需要将HttpServletResponse与您的扩展HttpServletResponseWrapper的类包装在一起:

public class StatusConvertingServletResponse extends HttpServletResponseWrapper {

    private int httpStatus;

    public StatusConvertingServletResponse(HttpServletResponse response) {
        super(response);
    }

    @Override
    public void setStatus(int sc) {
        httpStatus = sc;
        super.setStatus(sc);
    }

    @Override
    public int getStatus() {
        return httpStatus;
    }
}

并使用它:

StatusConvertingServletResponse res = new StatusConvertingServletResponse((HttpServletResponse)response);
chain.doFilter(request, res);
if (res.getStatus() == 200) {
   res.setStatus(n);
}

答案 1 :(得分:1)

只需在方法上方使用注释

@ResponseStatus(value=org.springframework.http.HttpStatus.OK)

@ResponseStatus(value=org.springframework.http.HttpStatus.CREATED)

@ResponseStatus(value=org.springframework.http.HttpStatus.OK)

@ResponseStatus(value=org.springframework.http.HttpStatus.OK)

@ResponseStatus(value=org.springframework.http.HttpStatus.NO_CONTENT)

答案 2 :(得分:0)

您可以延长春季@PostMapping并添加默认@ResponseStatus,即

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@PostMapping
@ResponseStatus(value = HttpStatus.CREATED)
public @interface CreatedPostMapping {
}

然后使用该注释而不是弹簧注释,例如

@CreatedPostMapping
public String create() { 
    return "blah"; 
}

与其他http动词注释类似。