Ver código fonte

自定义servlet

zhang 2 anos atrás
pai
commit
dec263e026

+ 2 - 0
src/main/java/com/api/ApiApplication.java

@@ -12,6 +12,7 @@ import org.springframework.boot.SpringApplication;
12 12
 import org.springframework.boot.autoconfigure.SpringBootApplication;
13 13
 import org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration;
14 14
 import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
15
+import org.springframework.boot.web.servlet.ServletComponentScan;
15 16
 import org.springframework.context.ConfigurableApplicationContext;
16 17
 import org.springframework.context.annotation.ComponentScan;
17 18
 import org.springframework.web.filter.CharacterEncodingFilter;
@@ -19,6 +20,7 @@ import org.springframework.web.servlet.DispatcherServlet;
19 20
 
20 21
 @SpringBootApplication
21 22
 @ComponentScan({"com.common","com.api"})
23
+@ServletComponentScan("com.common.servlet")
22 24
 public class ApiApplication {
23 25
 
24 26
 	public static void main(String[] args) {

+ 18 - 0
src/main/java/com/api/config/RegisterConfig.java

@@ -0,0 +1,18 @@
1
+package com.api.config;
2
+
3
+import com.common.servlet.MyServlet;
4
+import org.springframework.boot.web.servlet.FilterRegistrationBean;
5
+import org.springframework.boot.web.servlet.ServletRegistrationBean;
6
+import org.springframework.context.annotation.Bean;
7
+import org.springframework.context.annotation.Configuration;
8
+
9
+@Configuration
10
+public class RegisterConfig {
11
+    @Bean
12
+    public ServletRegistrationBean servletRegistrationBean() {
13
+        return new ServletRegistrationBean(new MyServlet(),"/psy");
14
+    }
15
+//    public FilterRegistrationBean filterRegistrationBean() {
16
+//        return new FilterRegistrationBean()
17
+//    }
18
+}

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

@@ -1,6 +1,7 @@
1 1
 package com.api.controller;
2 2
 
3 3
 import com.api.form.LoginForm;
4
+import com.common.exception.RRException;
4 5
 import org.springframework.stereotype.Controller;
5 6
 import org.springframework.ui.Model;
6 7
 import org.springframework.web.bind.annotation.*;
@@ -46,7 +47,12 @@ public class ModelAndMapController {
46 47
     }
47 48
     @PostMapping("postTest")
48 49
     @ResponseBody
49
-    public LoginForm postTest(@RequestBody LoginForm loginForm) {
50
+    public LoginForm postTest(
51
+            @RequestParam("company_id2") Integer companyId2,
52
+            @RequestParam("company_id") Integer companyId,
53
+                              LoginForm loginForm) {
54
+//        throw new RRException("aa");
55
+        int i = 10/0;
50 56
         return loginForm;
51 57
     }
52 58
 

+ 5 - 0
src/main/java/com/common/exception/RRExceptionHandler.java

@@ -14,6 +14,7 @@ import org.springframework.web.bind.MethodArgumentNotValidException;
14 14
 import org.springframework.web.bind.MissingServletRequestParameterException;
15 15
 import org.springframework.web.bind.annotation.ExceptionHandler;
16 16
 import org.springframework.web.bind.annotation.RestControllerAdvice;
17
+import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
17 18
 import org.springframework.web.servlet.NoHandlerFoundException;
18 19
 
19 20
 @Slf4j
@@ -110,6 +111,10 @@ public class RRExceptionHandler {
110 111
 //        logUtils.requestLog(e);
111 112
         return R.error("数据格式错误");
112 113
     }
114
+    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
115
+    public R handlerMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
116
+        return R.error("参数不匹配!");
117
+    }
113 118
 
114 119
     /**
115 120
      * 处理请求方法不支持异常

+ 23 - 0
src/main/java/com/common/filter/MyFilter.java

@@ -0,0 +1,23 @@
1
+package com.common.filter;
2
+
3
+import javax.servlet.*;
4
+import javax.servlet.annotation.WebFilter;
5
+import java.io.IOException;
6
+
7
+@WebFilter(urlPatterns = {"/my"})
8
+public class MyFilter implements Filter {
9
+    @Override
10
+    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
11
+
12
+    }
13
+
14
+    @Override
15
+    public void init(FilterConfig filterConfig) throws ServletException {
16
+        System.out.println("initMyFilter");
17
+    }
18
+
19
+    @Override
20
+    public void destroy() {
21
+        Filter.super.destroy();
22
+    }
23
+}

+ 18 - 0
src/main/java/com/common/servlet/MyServlet.java

@@ -0,0 +1,18 @@
1
+package com.common.servlet;
2
+
3
+import org.springframework.stereotype.Component;
4
+
5
+import javax.servlet.ServletException;
6
+import javax.servlet.annotation.WebServlet;
7
+import javax.servlet.http.HttpServlet;
8
+import javax.servlet.http.HttpServletRequest;
9
+import javax.servlet.http.HttpServletResponse;
10
+import java.io.IOException;
11
+
12
+//@WebServlet(urlPatterns = "/my")
13
+public class MyServlet extends HttpServlet {
14
+    @Override
15
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
16
+        resp.getWriter().write("myservlet");
17
+    }
18
+}

+ 1 - 0
src/main/resources/application.yml

@@ -36,6 +36,7 @@ spring:
36 36
   mvc:
37 37
     contentnegotiation:
38 38
       favor-parameter: true
39
+#    throw-exception-if-no-handler-found: true
39 40
 renren:
40 41
   redis:
41 42
     open: false  # 是否开启redis缓存  true开启   false关闭