Browse Source

矩阵变量注释 MatrixVariable使用 UrlPathHelper

zhang 2 years ago
parent
commit
05f44d4238

+ 14 - 1
src/main/java/com/api/ApiApplication.java

@@ -10,14 +10,27 @@ package com.api;
10
 
10
 
11
 import org.springframework.boot.SpringApplication;
11
 import org.springframework.boot.SpringApplication;
12
 import org.springframework.boot.autoconfigure.SpringBootApplication;
12
 import org.springframework.boot.autoconfigure.SpringBootApplication;
13
+import org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration;
14
+import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
15
+import org.springframework.context.ConfigurableApplicationContext;
13
 import org.springframework.context.annotation.ComponentScan;
16
 import org.springframework.context.annotation.ComponentScan;
17
+import org.springframework.web.filter.CharacterEncodingFilter;
18
+import org.springframework.web.servlet.DispatcherServlet;
14
 
19
 
15
 @SpringBootApplication
20
 @SpringBootApplication
16
 @ComponentScan({"com.common","com.api"})
21
 @ComponentScan({"com.common","com.api"})
17
 public class ApiApplication {
22
 public class ApiApplication {
18
 
23
 
19
 	public static void main(String[] args) {
24
 	public static void main(String[] args) {
20
-		SpringApplication.run(ApiApplication.class, args);
25
+		ConfigurableApplicationContext run = SpringApplication.run(ApiApplication.class, args);
26
+		WebMvcProperties bean = run.getBean(WebMvcProperties.class);
27
+		DispatcherServlet bean1 = run.getBean(DispatcherServlet.class);
28
+		CharacterEncodingFilter encodingFilter = run.getBean(CharacterEncodingFilter.class);
29
+		HttpEncodingAutoConfiguration bean2 = run.getBean(HttpEncodingAutoConfiguration.class);
30
+		String[] beanDefinitionNames = run.getBeanDefinitionNames();
31
+		for (String beanDefinitionName : beanDefinitionNames) {
32
+			//System.out.println(beanDefinitionName);
33
+		}
21
 	}
34
 	}
22
 
35
 
23
 }
36
 }

+ 2 - 0
src/main/java/com/api/config/DoudianConfig.java

@@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Value;
6
 import org.springframework.context.annotation.Bean;
6
 import org.springframework.context.annotation.Bean;
7
 import org.springframework.context.annotation.Configuration;
7
 import org.springframework.context.annotation.Configuration;
8
 import org.springframework.stereotype.Repository;
8
 import org.springframework.stereotype.Repository;
9
+import org.springframework.web.filter.CharacterEncodingFilter;
9
 
10
 
10
 import javax.annotation.PostConstruct;
11
 import javax.annotation.PostConstruct;
11
 
12
 
@@ -18,4 +19,5 @@ public class DoudianConfig {
18
     public AddressListRequest addressListRequest() {
19
     public AddressListRequest addressListRequest() {
19
         return new AddressListRequest();
20
         return new AddressListRequest();
20
     }
21
     }
22
+
21
 }
23
 }

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

@@ -14,7 +14,9 @@ import org.springframework.beans.factory.annotation.Autowired;
14
 import org.springframework.context.annotation.Configuration;
14
 import org.springframework.context.annotation.Configuration;
15
 import org.springframework.web.method.support.HandlerMethodArgumentResolver;
15
 import org.springframework.web.method.support.HandlerMethodArgumentResolver;
16
 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
16
 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
17
+import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
17
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
18
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
19
+import org.springframework.web.util.UrlPathHelper;
18
 
20
 
19
 import java.util.List;
21
 import java.util.List;
20
 
22
 
@@ -40,4 +42,11 @@ public class WebMvcConfig implements WebMvcConfigurer {
40
     public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
42
     public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
41
         argumentResolvers.add(loginUserHandlerMethodArgumentResolver);
43
         argumentResolvers.add(loginUserHandlerMethodArgumentResolver);
42
     }
44
     }
45
+
46
+    @Override
47
+    public void configurePathMatch(PathMatchConfigurer configurer) {
48
+        UrlPathHelper urlPathHelper = new UrlPathHelper();
49
+        urlPathHelper.setRemoveSemicolonContent(false);
50
+        configurer.setUrlPathHelper(urlPathHelper);
51
+    }
43
 }
52
 }

+ 12 - 7
src/main/java/com/api/controller/ApiTestController.java

@@ -9,6 +9,7 @@
9
 package com.api.controller;
9
 package com.api.controller;
10
 
10
 
11
 import com.api.dao.TokenDao;
11
 import com.api.dao.TokenDao;
12
+import com.api.query.TestQuery;
12
 import com.api.service.DoudianTokenService;
13
 import com.api.service.DoudianTokenService;
13
 import com.api.service.TokenService;
14
 import com.api.service.TokenService;
14
 import com.common.utils.R;
15
 import com.common.utils.R;
@@ -23,14 +24,12 @@ import io.swagger.annotations.ApiOperation;
23
 import org.springframework.beans.factory.BeanFactory;
24
 import org.springframework.beans.factory.BeanFactory;
24
 import org.springframework.beans.factory.annotation.Autowired;
25
 import org.springframework.beans.factory.annotation.Autowired;
25
 import org.springframework.context.ApplicationContext;
26
 import org.springframework.context.ApplicationContext;
26
-import org.springframework.web.bind.annotation.GetMapping;
27
-import org.springframework.web.bind.annotation.RequestAttribute;
28
-import org.springframework.web.bind.annotation.RequestMapping;
29
-import org.springframework.web.bind.annotation.RestController;
27
+import org.springframework.web.bind.annotation.*;
30
 import springfox.documentation.annotations.ApiIgnore;
28
 import springfox.documentation.annotations.ApiIgnore;
31
 
29
 
32
 import javax.servlet.http.HttpServletRequest;
30
 import javax.servlet.http.HttpServletRequest;
33
 import javax.servlet.http.HttpSession;
31
 import javax.servlet.http.HttpSession;
32
+import java.util.Map;
34
 
33
 
35
 /**
34
 /**
36
  * 测试接口
35
  * 测试接口
@@ -65,16 +64,22 @@ public class ApiTestController extends AbsctactController {
65
         return R.ok().put("userId", userId);
64
         return R.ok().put("userId", userId);
66
     }
65
     }
67
 
66
 
68
-    @GetMapping("notToken")
67
+    @GetMapping("notToken/{id}")
69
     @ApiOperation("忽略Token验证测试")
68
     @ApiOperation("忽略Token验证测试")
70
-    public R notToken(HttpServletRequest request, HttpSession httpSession){
69
+    public R notToken(@PathVariable("id") Integer id, HttpServletRequest request, HttpSession httpSession){
70
+        httpSession.setAttribute("aaa","go");
71
         Object beanNamesForType = applicationContext.getBean(TokenDao.class);
71
         Object beanNamesForType = applicationContext.getBean(TokenDao.class);
72
         TokenDao bean = beanFactory.getBean(TokenDao.class);
72
         TokenDao bean = beanFactory.getBean(TokenDao.class);
73
         TokenDao tokenDao = (TokenDao) beanNamesForType;
73
         TokenDao tokenDao = (TokenDao) beanNamesForType;
74
 //        return R.ok().put("msg", "无需token也能访问。。。");
74
 //        return R.ok().put("msg", "无需token也能访问。。。");
75
         return R.ok().put( "无需token也能访问。。。");
75
         return R.ok().put( "无需token也能访问。。。");
76
     }
76
     }
77
-
77
+    @GetMapping("notToken1")
78
+    @ApiOperation("忽略Token验证测试")
79
+    public R notToken1(@RequestParam Map<String,String> testQuery){
80
+//        return R.ok().put("msg", "无需token也能访问。。。");
81
+        return R.ok().put( "无需token也能访问。。。");
82
+    }
78
 //    @GetMapping("notToken")
83
 //    @GetMapping("notToken")
79
 //    @ApiOperation("忽略Token验证测试")
84
 //    @ApiOperation("忽略Token验证测试")
80
 //    public R createToken(HttpServletRequest request, HttpSession httpSession){
85
 //    public R createToken(HttpServletRequest request, HttpSession httpSession){

+ 36 - 0
src/main/java/com/api/controller/RequestController.java

@@ -0,0 +1,36 @@
1
+package com.api.controller;
2
+
3
+import org.springframework.stereotype.Controller;
4
+import org.springframework.web.bind.annotation.GetMapping;
5
+import org.springframework.web.bind.annotation.MatrixVariable;
6
+import org.springframework.web.bind.annotation.RequestAttribute;
7
+import org.springframework.web.bind.annotation.ResponseBody;
8
+
9
+import javax.servlet.http.HttpServletRequest;
10
+import java.util.List;
11
+
12
+@Controller
13
+public class RequestController {
14
+
15
+    @GetMapping("/goto")
16
+    public String goTo(HttpServletRequest request) {
17
+        request.setAttribute("zhang","xiaoyu");
18
+        return "forward:/success";
19
+    }
20
+    @GetMapping("/success")
21
+    @ResponseBody
22
+    public String success(@RequestAttribute("zhang") String zhang, HttpServletRequest request) {
23
+        return "我是";
24
+    }
25
+    // 矩阵变量的测试
26
+    // /matrix/mm;low=34;name=zhang,xiao,yu
27
+    // tip注意 springboot默认是关闭矩阵变量功能
28
+    @GetMapping("/matrix/{path}")
29
+    @ResponseBody
30
+    public String matrix(@MatrixVariable("low") String low,
31
+                         @MatrixVariable("name")List<String> names) {
32
+        System.out.println(low);
33
+        System.out.println(names);
34
+        return null;
35
+    }
36
+}

+ 2 - 0
src/main/java/com/api/entity/AccountEntity.java

@@ -1,5 +1,6 @@
1
 package com.api.entity;
1
 package com.api.entity;
2
 
2
 
3
+import com.api.enums.SexEnum;
3
 import com.baomidou.mybatisplus.annotation.TableField;
4
 import com.baomidou.mybatisplus.annotation.TableField;
4
 import com.baomidou.mybatisplus.annotation.TableId;
5
 import com.baomidou.mybatisplus.annotation.TableId;
5
 import com.baomidou.mybatisplus.annotation.TableName;
6
 import com.baomidou.mybatisplus.annotation.TableName;
@@ -14,5 +15,6 @@ public class AccountEntity {
14
     @TableField("user_name")
15
     @TableField("user_name")
15
     private String userName;
16
     private String userName;
16
     private Double balance;
17
     private Double balance;
18
+    private SexEnum sex;
17
     private Integer depId;
19
     private Integer depId;
18
 }
20
 }

+ 18 - 0
src/main/java/com/api/enums/SexEnum.java

@@ -0,0 +1,18 @@
1
+package com.api.enums;
2
+
3
+import com.baomidou.mybatisplus.annotation.EnumValue;
4
+import lombok.Getter;
5
+
6
+@Getter
7
+public enum SexEnum {
8
+    MALE(1,"男"),
9
+    FEMALE(2,"女");
10
+    @EnumValue
11
+    private Integer sex;
12
+    private String sexValue;
13
+
14
+    SexEnum(Integer sex,String sexValue) {
15
+        this.sex = sex;
16
+        this.sexValue = sexValue;
17
+    }
18
+}

+ 11 - 0
src/main/java/com/api/query/TestQuery.java

@@ -0,0 +1,11 @@
1
+package com.api.query;
2
+
3
+import com.fasterxml.jackson.annotation.JsonProperty;
4
+import lombok.Data;
5
+
6
+@Data
7
+public class TestQuery {
8
+    @JsonProperty("user_id")
9
+    private Integer userId;
10
+    private String userName;
11
+}

+ 4 - 2
src/main/resources/application.yml

@@ -30,7 +30,9 @@ spring:
30
         max-wait: -1ms      # 连接池最大阻塞等待时间(使用负值表示没有限制)
30
         max-wait: -1ms      # 连接池最大阻塞等待时间(使用负值表示没有限制)
31
         max-idle: 10      # 连接池中的最大空闲连接
31
         max-idle: 10      # 连接池中的最大空闲连接
32
         min-idle: 5       # 连接池中的最小空闲连接
32
         min-idle: 5       # 连接池中的最小空闲连接
33
-
33
+  http:
34
+    encoding:
35
+      charset: UTF-8
34
 renren:
36
 renren:
35
   redis:
37
   redis:
36
     open: false  # 是否开启redis缓存  true开启   false关闭
38
     open: false  # 是否开启redis缓存  true开启   false关闭
@@ -60,5 +62,5 @@ mybatis-plus:
60
     cache-enabled: false
62
     cache-enabled: false
61
     call-setters-on-nulls: true
63
     call-setters-on-nulls: true
62
     jdbc-type-for-null: 'null'
64
     jdbc-type-for-null: 'null'
63
-
65
+  type-enums-package: com.api.enums
64
 
66
 

+ 22 - 1
src/main/test/java/com/api/MybatisPlusTest2.java

@@ -1,9 +1,13 @@
1
 package com.api;
1
 package com.api;
2
 
2
 
3
+import com.api.dao.AccountMapper;
3
 import com.api.dao.ProductMapper;
4
 import com.api.dao.ProductMapper;
5
+import com.api.entity.AccountEntity;
4
 import com.api.entity.ProductEntity;
6
 import com.api.entity.ProductEntity;
7
+import com.api.enums.SexEnum;
5
 import org.junit.Test;
8
 import org.junit.Test;
6
 import org.junit.runner.RunWith;
9
 import org.junit.runner.RunWith;
10
+import org.springframework.beans.BeanUtils;
7
 import org.springframework.beans.factory.annotation.Autowired;
11
 import org.springframework.beans.factory.annotation.Autowired;
8
 import org.springframework.boot.test.context.SpringBootTest;
12
 import org.springframework.boot.test.context.SpringBootTest;
9
 import org.springframework.test.context.junit4.SpringRunner;
13
 import org.springframework.test.context.junit4.SpringRunner;
@@ -13,6 +17,8 @@ import org.springframework.test.context.junit4.SpringRunner;
13
 public class MybatisPlusTest2 {
17
 public class MybatisPlusTest2 {
14
     @Autowired
18
     @Autowired
15
     private ProductMapper productMapper;
19
     private ProductMapper productMapper;
20
+    @Autowired
21
+    private AccountMapper accountMapper;
16
     // 测试乐观锁
22
     // 测试乐观锁
17
     @Test
23
     @Test
18
     public void testOpt() {
24
     public void testOpt() {
@@ -22,6 +28,21 @@ public class MybatisPlusTest2 {
22
         int updateZhang = productMapper.updateById(productZhang);
28
         int updateZhang = productMapper.updateById(productZhang);
23
         productWang.setProductPrice(productWang.getProductPrice()-11);
29
         productWang.setProductPrice(productWang.getProductPrice()-11);
24
         int updateWang = productMapper.updateById(productWang);
30
         int updateWang = productMapper.updateById(productWang);
25
-
31
+        if(updateWang == 0) {
32
+            ProductEntity wangNew = productMapper.selectById(0);
33
+            wangNew.setProductPrice(wangNew.getProductPrice()-11);
34
+            productMapper.updateById(wangNew);
35
+        }
36
+    }
37
+    @Test
38
+    public void testEnum() {
39
+        AccountEntity addEntity = new AccountEntity();
40
+        addEntity.setUserName("limei");
41
+        addEntity.setDepId(2);
42
+        addEntity.setBalance(10000.0);
43
+        addEntity.setSex(SexEnum.FEMALE);
44
+        int insert = accountMapper.insert(addEntity);
26
     }
45
     }
46
+
47
+
27
 }
48
 }