Spring中的Web层常用的对象参数自动接受的方式

Spring中的常用的对象接受注解:

前端交互时遇到问题小结一下区别:

  • @RequestParam

是最常用的方式,发送的post(context-type="application/x-www-form-urlencodeed",其中这种方式都是浏览器默认的通用的方式,当然你提交的方式是可以修改的Context-Type的根式的,这种只能够接受一些简单的对象参数,例如单个的String demo,User user

     @RequestMapping(value = "demo",method = RequestMethod.POST)
    public void demo(Demo demoReq){
        log.info("demo");
    }

    @RequestMapping(value = "demo2",method = RequestMethod.POST)
    public void demo2(String code){
        log.info(code);
    }

其中Demo的对象中的只能为简单的类型的基础类型,不能嵌套对象或是List的集合类什么的复杂对象类型 

  • @RequestBody

(post提交方式为Context-type="application/json;charset=UTF-8"),这个主要重点,1.接受的时候不能有多个@Request

Body,只能通过一个对象去接受,2,请求的提交方式context-type类型,可以时json ,xml等(没有试过其他的)

 @RequestMapping(value = "getUserinfo",method = RequestMethod.POST)
    public void getUserInfoByID(@RequestBody DemoReq demoReq) {
        System.out.println(JSON.toJSON(demoReq));
        log.info("调用用户名调用接口");
        User userinfo = userRespository.queryUserById(1l);
        log.info("调用返回的接口: userInfo =[{}]", JSON.toJSON(userinfo));
    }

其中DemoReq:对象中嵌套list<Demo>Demo 对象中还可以嵌套对象

@Data
public class DemoReq {

    List<Demo> list;
}

其中有个前提时Spring配置中需要添加Spring的一个Mapping对象,还需要引入相应的jar包:

pom

<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.7.4</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.7.4</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.7.4</version>
    </dependency>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
    <!-- 1.配置jdbc文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" value="classpath:properties/config.properties"/>
    </bean>

    <!-- 2.扫描的包路径,这里不扫描被@Controller注解的类 --><!--使用<context:component-scan/> 可以不在配置<context:annotation-config/> -->
    <context:component-scan base-package="com.lks.demo">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <mvc:annotation-driven>
        <mvc:message-converters>
            <ref bean="stringHttpMessageConverter"/>
            <ref bean="mappingJackson2HttpMessageConverter"/>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
        <constructor-arg value="utf-8" index="0"></constructor-arg>
        <property name="supportedMediaTypes">
            <list>
                <value>text/plain;charset=UTF-8</value>
            </list>
        </property>
    </bean>

    <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>application/json;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <import resource="classpath*:spring/mybatis-config.xml"/>

</beans>

mappingJack2HttpMessageConverter类的方法注入到其中

  • @ParamVariabl

主要用于路径传参的方式,相当于在路径上占位符:

例如:Restful的方式,在get请求路径的修改

@RequestMapping(value = "/demo3/{code}/{demo}")
    public void demo4(@PathVariable(value = "code") String code, @PathVariable(value = "demo") String demo) {
        log.info(code);
    }

使用的时机

 

application/x-www-form-urlencodedapplication/json ,application/xmlmultipart/form-data单个参数对象 
@PathVariableGET、POST(并没有什么意义)GET、POST(并没有什么意义)GET、POST(并没有什么意义)  
      
@RequestHeaderGET、POSTGET、POSTGET、POST  
@CookieValueGET、POSTGET、POSTGET、POST  
@RequestParamGET、POST      ——       ——

    √

    ——
@RequestBodyGET、POSTGET、POST       ——  ——

 √

@ModelAttributeGET、POST      ——       ——

  ——

 √

@SessionAttributes     
      

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值