前后端传递参数不一致

描述:实体类有一个字段deviceId,前端传递参数为device_id
代码下载地址:https://download.youkuaiyun.com/download/activety/12462144
目录结构:
在这里插入图片描述

VO类

@Data
public class Vo{

    private Integer id;

    @ParamName("device_id")
    private String deviceId;

}

Controller类

@RestController
public class Controller {
    /**
     * @param job
     * @RequestParam这个注解非常重要
     */
    @RequestMapping("/aa")
    @ResponseBody
    public String test(@RequestParam Vo job) {
        System.out.println(job.toString());
        return job.toString();
    }
}

自定义注解ParamName

@Inherited
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ParamName {
    String value();
}

ParamNameDataBinder

import org.springframework.beans.MutablePropertyValues;
import org.springframework.web.servlet.mvc.method.annotation.ExtendedServletRequestDataBinder;

import javax.servlet.ServletRequest;
import java.util.Map;

public class ParamNameDataBinder extends ExtendedServletRequestDataBinder {

    private final Map<String, String> paramMappings;

    public ParamNameDataBinder(Object target, String objectName, Map<String, String> paramMappings) {
        super(target, objectName);
        this.paramMappings = paramMappings;
    }

    @Override
    protected void addBindValues(MutablePropertyValues mutablePropertyValues, ServletRequest request) {
        super.addBindValues(mutablePropertyValues, request);
        for (Map.Entry<String, String> entry : paramMappings.entrySet()) {
            String paramName = entry.getKey();
            String fieldName = entry.getValue();
            if (mutablePropertyValues.contains(paramName)) {
                mutablePropertyValues.add(fieldName, mutablePropertyValues.getPropertyValue(paramName).getValue());
            }
        }
    }
}

ParamNameProcessor

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ParamNameProcessor extends ServletModelAttributeMethodProcessor {

    @Autowired
    private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

    private static final Map<Class<?>, Map<String, String>> PARAM_MAPPINGS_CACHE = new ConcurrentHashMap<>(256);

    public ParamNameProcessor() {
        super(false);
    }

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(RequestParam.class)
                && !BeanUtils.isSimpleProperty(parameter.getParameterType())
                && Arrays.stream(parameter.getParameterType().getDeclaredFields())
                .anyMatch(field -> field.getAnnotation(ParamName.class) != null);
    }

    @Override
    protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest nativeWebRequest) {
        Object target = binder.getTarget();
        Map<String, String> paramMappings = this.getParamMappings(target.getClass());
        ParamNameDataBinder paramNameDataBinder = new ParamNameDataBinder(target, binder.getObjectName(), paramMappings);
        requestMappingHandlerAdapter.getWebBindingInitializer().initBinder(paramNameDataBinder, nativeWebRequest);
        super.bindRequestParameters(paramNameDataBinder, nativeWebRequest);
    }

    /**
     * @param targetClass
     * @return {@link Map<String, String>}
     */
    private Map<String, String> getParamMappings(Class<?> targetClass) {
        if (PARAM_MAPPINGS_CACHE.containsKey(targetClass)) {
            return PARAM_MAPPINGS_CACHE.get(targetClass);
        }
        Field[] fields = targetClass.getDeclaredFields();
        Map<String, String> paramMappings = new HashMap<>(32);
        for (Field field : fields) {
            ParamName paramName = field.getAnnotation(ParamName.class);
            if (paramName != null && !paramName.value().isEmpty()) {
                paramMappings.put(paramName.value(), field.getName());
            }
        }
        PARAM_MAPPINGS_CACHE.put(targetClass, paramMappings);
        return paramMappings;
    }

}

WebConfig

mport com.example.demo.annotation.ParamName;
import com.example.demo.annotation.ParamNameProcessor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class WebConfig {
    @Bean
    protected ParamNameProcessor paramNameProcessor() {
        return new ParamNameProcessor();
    }

    @Bean
    public BeanPostProcessor beanPostProcessor() {
        return new BeanPostProcessor() {

            @Override
            public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
                return bean;
            }

            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                if (bean instanceof RequestMappingHandlerAdapter) {
                    RequestMappingHandlerAdapter adapter = (RequestMappingHandlerAdapter) bean;
                    List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>(adapter.getArgumentResolvers());
                    argumentResolvers.add(0, paramNameProcessor());
                    adapter.setArgumentResolvers(argumentResolvers);
                }
                return bean;
            }
        };
    }
}

XML

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

测试结果:在这里插入图片描述

### 前后端请求参数传递方式 在前后端交互过程中,前端向后端发送数据通常采用HTTP协议中的GET或POST方法。以下是常见的参数传递方式及其特点: #### GET 请求 - **定义**: 参数通过URL地址栏输给服务器。 - **实现方式**: 使用`request.getParameter("参数名")`获取URL中携带的参数[^3]。 - **优点**: - 实现简单,适合少量、简单的参数传递。 - **缺点**: - URL长度有限制(一般超过2048字符),适合大量数据传递。 - 数据暴露于浏览器地址栏,存在安全隐患。 #### POST 请求 - **定义**: 参数封装在HTTP请求体中送给服务器。 - **实现方式**: 后台可以通过`@RequestParam`注解接收参数[^1];或者使用`request.getParameter("参数名")`来获取表单提交的数据。 - **优点**: - 支持大容量数据输。 - 数据会显示在URL中,相对安全。 - **缺点**: - 需要额外处理复杂的JSON或其他格式的数据。 --- ### 常见问题及解决方案 #### 1. 参数绑定失败 当请求参数无法正确映射到Controller方法的形参时,可能的原因如下: - 如果请求参数键与控制器方法形参名称一致,则需显式指定`@RequestParam`注解并提供对应的参数名[^2]。 - 示例代码: ```java @GetMapping("/findByUsername") public String findByUsername(@RequestParam(value = "user_name", required = false) String username){ return "用户名:" + username; } ``` #### 2. 接收复杂对象 如果需要接收多个参数组合成的对象,可以利用Spring MVC自动装配功能完成绑定。例如: ```java @PostMapping("/saveUser") public String saveUser(User user){ System.out.println(user); return "保存成功"; } ``` 此时,前端应按照实体类字段结构组织JSON字符串并通过POST请求发送[^4]。 #### 3. 处理默认值和可选参数 某些情况下,部分参数可能是非必填项,在这种情形下可通过设置`required=false`以及`defaultValue`属性解决: ```java @GetMapping("/searchArticle") public List<Article> searchArticle( @RequestParam(required = false, defaultValue = "") String category, @RequestParam(required = false, defaultValue = "") String title){ // 查询逻辑... return articleList; } ``` #### 4. 解决中文乱码问题 对于POST请求中的中文编码异常情况,可以在Web应用配置文件中加入过滤器统一设定字符集为UTF-8: ```xml <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> ``` --- ### 总结 前后端之间的参数传递主要依赖于HTTP协议下的GET和POST两种方式。实际开发中需要注意参数命名一致性、复杂对象组装规则以及特殊场景如缺省值处理等问题。上述提到的技术手段能够有效应对大部分日常需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值