Skip to content

Commit fd5470c

Browse files
committed
Merge branch 'dev'
2 parents e15c57d + 48d624e commit fd5470c

File tree

12 files changed

+282
-151
lines changed

12 files changed

+282
-151
lines changed

src/main/java/io/github/talelin/latticy/common/configuration/CommonConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public RequestLogInterceptor requestLogInterceptor() {
3030
/**
3131
* 新的分页插件,一缓和二缓遵循mybatis的规则
3232
* 需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
33-
* 参考链接:https://mp.baomidou.com/guide/interceptor.htm
33+
* 参考链接:https://mp.baomidou.com/guide/interceptor.html
3434
*/
3535
@Bean
3636
public MybatisPlusInterceptor mybatisPlusInterceptor() {
@@ -40,9 +40,10 @@ public MybatisPlusInterceptor mybatisPlusInterceptor() {
4040
}
4141

4242
/**
43-
* 参考链接:https://mp.baomidou.com/guide/interceptor.htm
43+
* 参考链接:https://mp.baomidou.com/guide/interceptor.html
4444
*/
4545
@Bean
46+
@SuppressWarnings("deprecation")
4647
public ConfigurationCustomizer configurationCustomizer() {
4748
return configuration -> configuration.setUseDeprecatedExecutor(false);
4849
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.github.talelin.latticy.common.configuration;
2+
3+
import org.springframework.util.Assert;
4+
import org.springframework.web.bind.ServletRequestDataBinder;
5+
import org.springframework.web.bind.WebDataBinder;
6+
import org.springframework.web.context.request.NativeWebRequest;
7+
import org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor;
8+
9+
import javax.servlet.ServletRequest;
10+
11+
/**
12+
* @author Gadfly
13+
*/
14+
15+
public class CustomServletModelAttributeMethodProcessor extends ServletModelAttributeMethodProcessor {
16+
17+
public CustomServletModelAttributeMethodProcessor(final boolean annotationNotRequired) {
18+
super(annotationNotRequired);
19+
}
20+
21+
@Override
22+
protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
23+
ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);
24+
Assert.state(servletRequest != null, "No ServletRequest");
25+
ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
26+
27+
// ServletModelAttributeMethodProcessor 此处使用的 servletBinder.bind(servletRequest)
28+
// 修改的目的是为了将 ServletRequestDataBinder 换成自定的 CustomServletRequestDataBinder
29+
new CustomServletRequestDataBinder(servletBinder.getTarget()).bind(servletRequest);
30+
}
31+
32+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.github.talelin.latticy.common.configuration;
2+
3+
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
4+
import org.springframework.beans.MutablePropertyValues;
5+
import org.springframework.beans.PropertyValue;
6+
import org.springframework.web.bind.ServletRequestDataBinder;
7+
8+
import javax.servlet.ServletRequest;
9+
import java.util.LinkedList;
10+
import java.util.List;
11+
12+
/**
13+
* @author Gadfly
14+
*/
15+
16+
public class CustomServletRequestDataBinder extends ServletRequestDataBinder {
17+
18+
public CustomServletRequestDataBinder(final Object target) {
19+
super(target);
20+
}
21+
22+
@Override
23+
protected void addBindValues(MutablePropertyValues mpvs, ServletRequest request) {
24+
List<PropertyValue> pvs = mpvs.getPropertyValueList();
25+
List<PropertyValue> adds = new LinkedList<>();
26+
for (PropertyValue pv : pvs) {
27+
String name = pv.getName();
28+
String camel = StringUtils.underlineToCamel(name);
29+
if (!name.equals(camel)) {
30+
adds.add(new PropertyValue(camel, pv.getValue()));
31+
}
32+
}
33+
pvs.addAll(adds);
34+
}
35+
36+
}

src/main/java/io/github/talelin/latticy/common/configuration/WebConfiguration.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.beans.factory.annotation.Value;
1010
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
1112
import org.springframework.web.servlet.config.annotation.CorsRegistry;
1213
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
1314
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
1415
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
1516

1617
import java.nio.file.FileSystems;
1718
import java.nio.file.Path;
19+
import java.util.List;
1820

1921
/**
2022
* Spring MVC 配置
@@ -83,6 +85,14 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
8385
.addResourceLocations("file:" + getAbsDir() + "/");
8486
}
8587

88+
/**
89+
* request parameter 转 java bean 时 snake_case 转 camelCase
90+
*/
91+
@Override
92+
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
93+
resolvers.add(new CustomServletModelAttributeMethodProcessor(true));
94+
}
95+
8696
private String getDirServePath() {
8797
// assets/**
8898
// assets/

0 commit comments

Comments
 (0)