首页 > 编程语言 > 为spring get请求添加自定义的参数处理操作(如下划线转驼峰)
2020
10-10

为spring get请求添加自定义的参数处理操作(如下划线转驼峰)

1.生成自己的注解(为了确定在哪些位置使用)

/**
 * 关闭patch delete的model处理,否则会报错
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AliasProcessor {
}
/**
 * 处理Get 请求参数的驼峰问题
 * @author lw
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ValueFrom {
 /**
 * 参数名(别名)列表
 */
 String[] value();
}

2.实现自己的ServletModelAttributeMethodProcessor

/**
 * 为了减少使用 @RequestPath 将get参数封装到实体类中 重写ModelAttributeMethodProcessor
 * 注:由于get请求为非raw请求,spring默认使用@ModelArrtribute注解,不会自动将下划线的数据转为驼峰数据
 * 所以需要自定义一个处理器,进行该操作 *
 * @author lw
 */

public class AliasModelAttributeMethodProcessor extends ServletModelAttributeMethodProcessor {
 private ApplicationContext applicationContext;

 /**
 * 过滤掉patch请求,防止报错
 */
 @Override
 public boolean supportsParameter(MethodParameter parameter) {
 return parameter.getMethodAnnotation(AliasProcessor.class)!=null;
 }

 public AliasModelAttributeMethodProcessor(ApplicationContext applicationContext) {
 super(true);
 this.applicationContext=applicationContext;
 }

 @Override
 protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
 AliasDataBinder aliasBinder = new AliasDataBinder(binder.getTarget(), binder.getObjectName());
 RequestMappingHandlerAdapter requestMappingHandlerAdapter = this.applicationContext.getBean(RequestMappingHandlerAdapter.class);
 requestMappingHandlerAdapter.getWebBindingInitializer().initBinder(aliasBinder);
 aliasBinder.bind(request.getNativeRequest(ServletRequest.class));
 }
}

3.自己的数据处理类

/**
 * 重新数据处理类
 * @author lw
 */
public class AliasDataBinder extends ExtendedServletRequestDataBinder {

 public AliasDataBinder(Object target, String objectName) {
 super(target, objectName);
 }

 /**
 * 复写addBindValues方法
 * @param mpvs 这里面存的就是请求参数的key-value对
 * @param request 请求本身, 这里没有用到
 */
 @Override
 protected void addBindValues(MutablePropertyValues mpvs, ServletRequest request) {
 super.addBindValues(mpvs, request);
 // 处理要绑定参数的对象
 Class<?> targetClass = getTarget().getClass();
 // 获取对象的所有字段(拿到Test类的字段)
 Field[] fields = targetClass.getDeclaredFields();
 // 处理所有字段
 for (Field field : fields) {
  // 原始字段上的注解
  ValueFrom valueFromAnnotation = field.getAnnotation(ValueFrom.class);
  // 若参数中包含原始字段或者字段没有别名注解, 则跳过该字段
  if (mpvs.contains(field.getName()) || valueFromAnnotation == null) {
  continue;
  }
  // 参数中没有原始字段且字段上有别名注解, 则依次取别名列表中的别名, 在参数中最先找到的别名的值赋值给原始字段
  for (String alias : valueFromAnnotation.value()) {
  // 若参数中包含该别名, 则把别名的值赋值给原始字段
  if (mpvs.contains(alias)) {
   // 给原始字段赋值
   mpvs.add(field.getName(), mpvs.getPropertyValue(alias).getValue());
   // 跳出循环防止取其它别名
   break;
  }
  }
 }
 }
}

4.注册到spring中

/**
 * 为了获得context需要实现ApplicationContextAware接口
 * @author lw
 */
@Configuration
public class WebmvcConfig implements ApplicationContextAware {

 @Autowired
 private RequestMappingHandlerAdapter adapter;

 private ApplicationContext applicationContext = null;

 @Override
 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
 this.applicationContext=applicationContext;
 }

 /**
 * 将自定义的processor添加到adapter中
 */
 @PostConstruct
 protected void injectSelfMethodArgumentResolver() {
 List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();
 argumentResolvers.add(new AliasModelAttributeMethodProcessor(this.applicationContext));
 argumentResolvers.addAll(adapter.getArgumentResolvers());
 adapter.setArgumentResolvers(argumentResolvers);
 }
}

补充知识:springboot - mybatis - 下划线与驼峰自动转换 mapUnderscoreToCamelCase

以前都是在mybatis.xml中来配置,但是spring boot不想再用xml配置文件。网上搜寻了好久,才找到设置办法:

sessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);

db配置文件源码:

package com.vip.qa.vop.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
import java.util.Properties;

/**
 * Created by danny.yao on 2017/10/25.
 */
@Configuration
@MapperScan(basePackages = VOPDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "vopSqlSessionFactory")
public class VOPDataSourceConfig {
 static final String PACKAGE = "com.vip.qa.vop.mapper.vop";

 @Value("${vop.datasource.url}")
 private String dbUrl;

 @Value("${vop.datasource.username}")
 private String dbUser;

 @Value("${vop.datasource.password}")
 private String dbPassword;

 @Value("${vop.datasource.driver-class-name}")
 private String dbDriver;

 @Bean(name = "vopDataSource")
 @Qualifier
 @Primary
 public DataSource vopDataSource() {
 DruidDataSource dataSource = new DruidDataSource();
 dataSource.setDriverClassName(dbDriver);
 dataSource.setUrl(dbUrl);
 dataSource.setUsername(dbUser);
 dataSource.setPassword(dbPassword);
 return dataSource;
 }

 @Bean(name = "vopSqlSessionFactory")
 @Qualifier
 @Primary
 public SqlSessionFactory vopSqlSessionFactory(@Qualifier("vopDataSource") DataSource scepDataSource) throws Exception {
 final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
 sessionFactoryBean.setDataSource(scepDataSource);

 PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
 sessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/vop/*.xml"));
 sessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);

 return sessionFactoryBean.getObject();
 }

// @Bean(name = "vopTransactionManager")
// @Qualifier
// public DataSourceTransactionManager testDataTransactionManager() {
// return new DataSourceTransactionManager(vopDataSource());
// }

}

以上这篇为spring get请求添加自定义的参数处理操作(如下划线转驼峰)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持自学编程网。

编程技巧