美文网首页
springmvc 最简配置-处理json数据

springmvc 最简配置-处理json数据

作者: jarvan4dev | 来源:发表于2016-02-25 15:24 被阅读2297次

    为什么会有这篇文章,因为在处理过程中发现springmvc不能处理json,这不可能啊,所以写了下,发现以前没有重视@RequestBody。

    • 注: 注意键值对和json的区别:
      JSON:
    {"username":"zhangsan"}
    

    键值对:

    Paste_Image.png

    Controller

    import com.zhb.entity.Account;
    import com.zhb.entity.User;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * Created by jarvan4dev on 16/2/25.
     */
    @RestController
    public class IndexController {
    
        @RequestMapping("/user")
        public Account getUser(@RequestBody User user){
            return new Account(1,user.getUsername(),"6227001376550927913", 10000.0);
        }
    }
    

    实体类

    import lombok.Data;
    
    /**
     * Created by jarvan4dev on 16/2/25.
     */
    @Data //@Data-请参看我的lombok介绍
    public class User {
        private int id;
        private String username;
        private String password;
    }
    
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    /**
     * Created by jarvan4dev on 16/2/25.
     */
    @Data
    @AllArgsConstructor
    public class Account {
    
        private int id;
        private String username;
        private String cardNo;
        private double balance;
    }
    

    pom.xml

    <dependencies>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-context-support</artifactId>
              <version>4.2.0.RELEASE</version>
          </dependency>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-webmvc</artifactId>
              <version>4.2.0.RELEASE</version>
          </dependency>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-aspects</artifactId>
              <version>4.2.0.RELEASE</version>
          </dependency>
          <dependency>
              <groupId>org.aspectj</groupId>
              <artifactId>aspectjweaver</artifactId>
              <version>1.8.6</version>
          </dependency>
          <dependency>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
              <version>1.16.6</version>
          </dependency>
          <dependency>
              <groupId>com.alibaba</groupId>
              <artifactId>fastjson</artifactId>
              <version>1.2.7</version>
          </dependency>
          <dependency>
              <groupId>commons-fileupload</groupId>
              <artifactId>commons-fileupload</artifactId>
              <version>1.3.1</version>
          </dependency>
          <dependency>
              <groupId>log4j</groupId>
              <artifactId>log4j</artifactId>
              <version>1.2.17</version>
          </dependency>
      </dependencies>
    

    spring-mvc.xml

    <?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <context:component-scan base-package="com.zhb.controller" />
    
        <mvc:annotation-driven>
            <mvc:message-converters>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <constructor-arg value="UTF-8" />
                </bean>
                <bean
                        class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                    <property name="features">
                        <array value-type="com.alibaba.fastjson.serializer.SerializerFeature">
                            <value>NotWriteDefaultValue</value>
                            <value>DisableCircularReferenceDetect</value>
                        </array>
                    </property>
                    <property name="supportedMediaTypes">
                        <list>
                            <value>application/json;charset=UTF-8</value>
                            <value>text/html;charset=UTF-8</value>
                            <value>text/plain;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>
    
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="defaultEncoding" value="UTF-8"/>
            <property name="maxUploadSize" value="540000"/>
        </bean>
    </beans>
    

    web.xml

    <!DOCTYPE web-app PUBLIC
            "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
            "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
        <display-name>Archetype Created Web Application</display-name>
        <servlet>
            <servlet-name>spring</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-mvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>spring</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    </web-app>
    

    使用curl测试:

    curl localhost:8080/sp/user -d '{"id":1,"username":"zhangsan","password":"123"}' -H "Content-type: application/json" 
    
    • 注意: 为了让springmvc处理json,需要在Controller的参数中加入 @RequestBody注解,
      如:
    ... getUser(@RequestBody User user){...}
    

    相关文章

      网友评论

          本文标题:springmvc 最简配置-处理json数据

          本文链接:https://www.haomeiwen.com/subject/mrgikttx.html