zhang před 2 roky
rodič
revize
232ded6dd9

+ 3 - 0
src/main/java/com/api/config/WebMvcConfig.java

@@ -12,6 +12,7 @@ import com.api.converter.CustomMessageConverter;
12 12
 import com.api.form.ChildConvertForm;
13 13
 import com.common.interceptor.AuthorizationInterceptor;
14 14
 import com.api.resolver.LoginUserHandlerMethodArgumentResolver;
15
+import com.common.interceptor.TestInterceptor;
15 16
 import org.springframework.beans.factory.annotation.Autowired;
16 17
 import org.springframework.context.annotation.Configuration;
17 18
 import org.springframework.core.convert.converter.Converter;
@@ -46,6 +47,7 @@ public class WebMvcConfig implements WebMvcConfigurer {
46 47
     @Override
47 48
     public void addInterceptors(InterceptorRegistry registry) {
48 49
         registry.addInterceptor(authorizationInterceptor).addPathPatterns("/api/**");
50
+        registry.addInterceptor(new TestInterceptor()).addPathPatterns("/interceptor/**");
49 51
     }
50 52
 
51 53
     @Override
@@ -90,6 +92,7 @@ public class WebMvcConfig implements WebMvcConfigurer {
90 92
         stringMediaTypeHashMap.put("zxy",MediaType.parseMediaType("application/x-zxy"));
91 93
         ParameterContentNegotiationStrategy parameterContentNegotiationStrategy =
92 94
                 new ParameterContentNegotiationStrategy(stringMediaTypeHashMap);
95
+        parameterContentNegotiationStrategy.setParameterName("dataFormat");
93 96
         configurer.strategies(Arrays.asList(parameterContentNegotiationStrategy));
94 97
     }
95 98
 }

+ 19 - 0
src/main/java/com/api/controller/InterceptorTestController.java

@@ -0,0 +1,19 @@
1
+package com.api.controller;
2
+
3
+import com.common.utils.R;
4
+import org.springframework.stereotype.Controller;
5
+import org.springframework.web.bind.annotation.GetMapping;
6
+import org.springframework.web.bind.annotation.RequestMapping;
7
+import org.springframework.web.bind.annotation.RequestParam;
8
+import org.springframework.web.bind.annotation.ResponseBody;
9
+
10
+@Controller
11
+@RequestMapping("/interceptor")
12
+public class InterceptorTestController {
13
+
14
+    @GetMapping("test1")
15
+    @ResponseBody
16
+    public R test1(@RequestParam("param") String param) {
17
+        return R.ok("ok");
18
+    }
19
+}

+ 1 - 1
src/main/java/com/api/controller/ModelAndMapController.java

@@ -46,7 +46,7 @@ public class ModelAndMapController {
46 46
     }
47 47
     @PostMapping("postTest")
48 48
     @ResponseBody
49
-    public LoginForm postTest(LoginForm loginForm) {
49
+    public LoginForm postTest(@RequestBody LoginForm loginForm) {
50 50
         return loginForm;
51 51
     }
52 52
 

+ 24 - 0
src/main/java/com/common/interceptor/TestInterceptor.java

@@ -0,0 +1,24 @@
1
+package com.common.interceptor;
2
+
3
+import com.common.exception.RRException;
4
+import org.springframework.web.servlet.HandlerInterceptor;
5
+import org.springframework.web.servlet.ModelAndView;
6
+
7
+import javax.servlet.http.HttpServletRequest;
8
+import javax.servlet.http.HttpServletResponse;
9
+
10
+public class TestInterceptor implements HandlerInterceptor {
11
+    @Override
12
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
13
+        System.out.println(request.getRequestURI());
14
+        if(request.getParameter("name").equals("lanjie")) {
15
+            throw new RRException("已被TestInterceptor拦截");
16
+        }
17
+        return true;
18
+    }
19
+
20
+    @Override
21
+    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
22
+        System.out.println(response.getContentType());
23
+    }
24
+}