1.全局响应增强处理器

添加注解扫描controller层

  1. @RestControllerAdvice(basePackages = {"com.cyou.yuffieserver2.controller"})

编写全局响应增强器

  1. import com.cyou.yuffieserver3.core.result.ResultData;
  2. import org.springframework.core.MethodParameter;
  3. import org.springframework.http.HttpStatus;
  4. import org.springframework.http.MediaType;
  5. import org.springframework.http.converter.HttpMessageConverter;
  6. import org.springframework.http.server.ServerHttpRequest;
  7. import org.springframework.http.server.ServerHttpResponse;
  8. import org.springframework.web.bind.annotation.RestControllerAdvice;
  9. import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
  10. /**
  11. * @author 冯铁城 [fengtiecheng@cyou-inc.com]
  12. * @date 2022-01-11 11:00:11
  13. * @describe:全局RestController请求返回处理
  14. */
  15. @RestControllerAdvice(basePackages = {"com.cyou.yuffieserver3"})
  16. public class GlobalResponseControllerAdvice implements ResponseBodyAdvice<Object> {
  17. /**
  18. * 判断是否要执行beforeBodyWrite方法,true为执行,false不执行
  19. *
  20. * @param returnType 返回的类型
  21. * @param aClass 我也不知道这个参数咋用
  22. * @return true-跳转到beforeBodyWrite/false-直接返回
  23. */
  24. @Override
  25. public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> aClass) {
  26. return true;
  27. }
  28. /**
  29. * 会拦截controller层的返回,在响应写入响应体之前执行的方法
  30. *
  31. * @param data 响应数据
  32. * @param returnType 返回的类型
  33. * @param mediaType 我也不知道这个参数咋用
  34. * @param aClass 我也不知道这个参数咋用
  35. * @param serverHttpRequest HTTP请求
  36. * @param serverHttpResponse HTTP响应
  37. * @return 处理过后的响应数据
  38. */
  39. @Override
  40. public Object beforeBodyWrite(Object data, MethodParameter returnType, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
  41. //1.data转型
  42. ResultData resultData = (ResultData) data;
  43. //2.获取响应码
  44. int code = resultData.getCode();
  45. //3.设置响应码
  46. serverHttpResponse.setStatusCode(HttpStatus.valueOf(code));
  47. return resultData;
  48. }
  49. }

2.全局请求去除参数前后空格处理器

  1. import com.fasterxml.jackson.core.JsonParser;
  2. import com.fasterxml.jackson.databind.DeserializationContext;
  3. import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
  4. import org.springframework.beans.propertyeditors.StringTrimmerEditor;
  5. import org.springframework.boot.autoconfigure.AutoConfigureAfter;
  6. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  7. import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
  8. import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
  9. import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12. import org.springframework.util.StringUtils;
  13. import org.springframework.web.bind.WebDataBinder;
  14. import org.springframework.web.bind.annotation.ControllerAdvice;
  15. import org.springframework.web.bind.annotation.InitBinder;
  16. import org.springframework.web.servlet.DispatcherServlet;
  17. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  18. import javax.servlet.Servlet;
  19. import java.io.IOException;
  20. import java.util.TimeZone;
  21. /**
  22. * @author 冯铁城 [fengtiecheng@cyou-inc.com]
  23. * @date 2022-01-11 11:28:32
  24. * @describe:全局参数trim处理
  25. */
  26. @Configuration
  27. @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
  28. @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
  29. @AutoConfigureAfter(WebMvcAutoConfiguration.class)
  30. public class GlobalParamTrimHandler {
  31. /**
  32. * 将请求URL里面的参数进行trim转换
  33. */
  34. @ControllerAdvice
  35. public static class ControllerStringParamTrimConfig {
  36. @InitBinder
  37. public void initBinder(WebDataBinder binder) {
  38. binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
  39. }
  40. }
  41. /**
  42. * 将请求体中的参数进行trim转换
  43. *
  44. * @return
  45. */
  46. @Bean
  47. public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
  48. return jacksonObjectMapperBuilder -> {
  49. jacksonObjectMapperBuilder.timeZone(TimeZone.getDefault());
  50. jacksonObjectMapperBuilder.deserializerByType(String.class, new StdScalarDeserializer<String>(String.class) {
  51. @Override
  52. public String deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException {
  53. return StringUtils.trimWhitespace(jsonParser.getValueAsString());
  54. }
  55. });
  56. };
  57. }
  58. }