码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC(十三):SpringMVC 与fastjson集成

时间:2018-01-31 14:28:18      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:blog   control   res   repo   def   guest   work   jar包下载   fast   

1)fastjson jar包下载地址:https://sourceforge.net/projects/fastjson/
下载完成后需要把jar包拷贝到WEB-INF/lib文件夹中。
2)使用pom.xml配置:
如果你使用了Maven,maven repository配置如下:

<repository>
    <id>opensesame</id>
    <name>Alibaba OpenSource Repsoitory</name>
    <url>http://code.alibabatech.com/mvn/releases/</url>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
</repository>

pom.xml文件中加入依赖依赖:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.1.34</version>
</dependency>

使用springmvc的情况下,需要在springmvc.xml中添加配置信息: 

    <!-- 基于Annotation的映射方式 -->  
    <mvc:annotation-driven >  
         <mvc:message-converters register-defaults="true">  
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">  
                <constructor-arg value="UTF-8" />  
            </bean>  
            <!-- <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
                 <property name="supportedMediaTypes">  
                    <list>  
                        <value>text/html;charset=UTF-8</value>  
                    </list>  
                </property>  
            </bean> -->  
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">    
                <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>    
                <property name="features">    
                    <array>    
                        <value>WriteMapNullValue</value>    
                        <value>WriteNullStringAsEmpty</value>    
                    </array>    
                </property>    
           </bean>  
        </mvc:message-converters>  
    </mvc:annotation-driven>  

测试类:

 1 public class User {
 2     private Long id;
 3     private String name;
 4 
 5     public Long getId() { return id; }
 6     public void setId(Long id) { this.id = id; }
 7 
 8     public String getName() { return name; }
 9     public void setName(String name) { this.name = name; }
10 }
11 public class Group {
12     private Long id;
13     private String name;
14     private List<User> users = new ArrayList<User>();
15 
16     public Long getId() { return id; }
17     public void setId(Long id) { this.id = id; }
18 
19     public String getName() { return name; }
20     public void setName(String name) { this.name = name; }
21 
22     public List<User> getUsers() { return users; }
23     public void setUsers(List<User> users) { this.users = users; }
24 }
25 
26 import com.alibaba.fastjson.JSON;
27 
28 Group group = new Group();
29 group.setId(0L);
30 group.setName("admin");
31 
32 User guestUser = new User();
33 guestUser.setId(2L);
34 guestUser.setName("guest");
35 
36 User rootUser = new User();
37 rootUser.setId(3L);
38 rootUser.setName("root");
39 
40 group.getUsers().add(guestUser);
41 group.getUsers().add(rootUser);
42 
43 String jsonString = JSON.toJSONString(group);
44 System.out.println(jsonString);
45 
46 Group group2 = JSON.parseObject(jsonString, Group.class);

SpringMVC中使用:

package com.dx.springlearn.hanlders;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dx.springlearn.entities.Account;
import com.dx.springlearn.entities.WSUserInfo;


@Controller
public class TestModelData {
    private final String SUCCESS = "success";

    @RequestMapping(name = "/postUser", method = RequestMethod.POST)
    public void postUser(String openid, String lng, String lat, HttpServletRequest request,
            HttpServletResponse response) {
        System.out.println("post user..." + openid + "," + lat + "," + lng);

    }

    @RequestMapping(name = "/getUserList", method = RequestMethod.GET)
    public void getUserList(String lng, String lat, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("get user list...." + lat + "," + lng);
        
        List<WSUserInfo> userItems = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            userItems.add(new WSUserInfo("openid" + i, "120.082" + i, "30.238" + i));
        }

        try {
            //String jsonlist="[{\"openid\":\"openid0\",\"longitude\":\"120.0820\",\"latitude\":\"30.2380\"},{\"openid\":\"openid1\",\"longitude\":\"120.0821\",\"latitude\":\"30.2381\"}]";
            String jsonString = JSON.toJSONString(userItems);
            
            PrintWriter out = response.getWriter();
            out.print(jsonString);
            out.flush();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

package com.dx.springlearn.entities;

public class WSUserInfo {
    private String openid;
    private String longitude;
    private String latitude;

    public WSUserInfo(String openid, String longitude, String latitude) {
        super();
        this.openid = openid;
        this.longitude = longitude;
        this.latitude = latitude;
    }

    public String getOpenid() {
        return openid;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }
}

 

SpringMVC(十三):SpringMVC 与fastjson集成

标签:blog   control   res   repo   def   guest   work   jar包下载   fast   

原文地址:https://www.cnblogs.com/yy3b2007com/p/8391314.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!