如何在Spring中添加HTTP生命周期中间件处理程序?

bod*_*ser 4 java spring spring-mvc

我有一个CustomerController.java

package com.satisfeet.http;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.satisfeet.core.model.Address;
import com.satisfeet.core.model.Customer;
import com.satisfeet.core.service.CustomerService;

@RestController
@RequestMapping("/customers")
public class CustomerController {

    @Autowired
    private CustomerService service;

    @RequestMapping(method = RequestMethod.GET)
    public Iterable<Customer> index() {
        return this.service.list();
    }

    @RequestMapping(method = RequestMethod.POST)
    public Customer create(@RequestBody Customer customer) {
        this.service.create(customer);

        return customer;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public Customer show(@PathVariable Integer id) {
        return this.service.show(id);
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/{id}")
    public void update(@PathVariable Integer id, @RequestBody Customer customer) {
        this.service.update(id, customer);
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public void destroy(@PathVariable Integer id) {
        this.service.delete(id);
    }

}
Run Code Online (Sandbox Code Playgroud)

ExceptionController.java

package com.satisfeet.http;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.satisfeet.core.exception.NotFoundException;

@ControllerAdvice
public class ExceptionController {

    @ExceptionHandler(NotFoundException.class)
    public ResponseEntity notFoundError() {
        return new ResponseEntity(HttpStatus.NOT_FOUND);
    }

}
Run Code Online (Sandbox Code Playgroud)

我现在想添加某种HTTP请求-响应中间件,该响应在编写响应之前执行,并将HTTP状态代码写入json:

HTTP/1.1 404 OK
Connection: close
Content-Type: application/json

{"error":"not found"}
Run Code Online (Sandbox Code Playgroud)

我知道如何将a转换HttpStatus为a,String但我不知道在哪里可以使用全局转换@ControllerAdvice

那么,如何注册可以访问响应对象的全局处理程序?

小智 6

我认为你正在寻找春天 Interceptor

请查看本教程,该教程描述了如何使用拦截器。

使用HandlerInterceptor检查spring文档的拦截请求

终于在这里检查这个答案

希望能有所帮助。

  • 顺便说一句,教程链接已失效。使用 https://www.digitalocean.com/community/tutorials/spring-mvc-interceptor-example-handlerinterceptor-handlerinterceptoradapter 代替。但是,不应使用链接教程的内容,因为 HandlerInterceptorAdaptor 已弃用 (2认同)