springmvc接口接收json類型參數設置


Springmvc需要如下配置:

1.開啟注解

<!-- 開啟注解-->  
 <mvc:annotation-driven />  

2.加入相關bean

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
    <property name="messageConverters">  
        <list>  
            <ref bean="jsonHttpMessageConverter" />  
        </list>  
    </property>  
</bean>  
  
<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
    <property name="supportedMediaTypes">  
        <list>  
            <value>application/json;charset=UTF-8</value>  
        </list>  
    </property>  
</bean>  

3.maven加入jackson依賴

   <dependency>  
    <groupId>com.fasterxml.jackson.core</groupId>  
    <artifactId>jackson-core</artifactId>  
    <version>2.5.2</version>  
</dependency>  
  
<dependency>  
    <groupId>com.fasterxml.jackson.core</groupId>  
    <artifactId>jackson-databind</artifactId>  
    <version>2.5.2</version>  
</dependency>  

4.controller層入參要加注解@RequestBody

必須是個類啊啊啊啊,不能用HttpServletRequest了,它不是個json

 

@RequestBody 接受對象里面,如果已經有了非空構造函數,那么它同時必須要有默認的空構造函數,注意這個構造函數一定要為空


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM