Jackson
- 导入Jackson的依赖:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0</version>
</dependency>
页面乱码问题spring配置文件中配置JSON解决
<!--JSON乱码问题配置-->
<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="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
Jackson的使用例子:
package com.huang.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huang.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
@Controller
//@RestController 注解下类中的方法不会走视图解析器,只会返回字符串
public class JsonController {
@ResponseBody//用这个注释来注释一个方法,会直接返回一个字符串,就不会走视图解析器了
@RequestMapping("/j1")
public String test() throws JsonProcessingException {
//创建一个jackson对象
ObjectMapper mapper = new ObjectMapper();
User user = new User(1,"老八",15);
//转换实体类为JSON字符串
String str = mapper.writeValueAsString(user);
return str;
}
@RequestMapping("/j2")
@ResponseBody
public String test2() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
List<User> userList = new ArrayList<User>();
User user = new User(1,"老八",15);
User user2 = new User(2,"老郭",157);
User user3 = new User(3,"老giao",158);
User user4 = new User(4,"老二",159);
userList.add(user);
userList.add(user2);
userList.add(user3);
userList.add(user4);
String s = mapper.writeValueAsString(userList);
return s;
}
}
注意点:@RestController 注解下类中的方法不会走视图解析器,只会返回字符串
@ResponseBody 用这个注释来注释一个方法,会直接返回一个字符串,就不会走视图解析器了
Fastjson
- 导入Fastjson依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
Fastjson使用实例:
package com.huang.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huang.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class FastJson {
@RequestMapping("/fj1")
public String test(){
List<User> userList = new ArrayList<User>();
User user1 = new User(1,"老八",15);
User user2 = new User(2,"老郭",157);
User user3 = new User(3,"老giao",158);
User user4 = new User(4,"老二",159);
userList.add(user1);
userList.add(user2);
userList.add(user3);
userList.add(user4);
System.out.println("********JAVA对象 转 JSON字符串********");
String str1 = JSON.toJSONString(userList);
System.out.println("JSON.tiJSONString(userList)==>"+str1);
String str2 = JSON.toJSONString(user1);
System.out.println("JSON.tiJSONString(user1)==>"+str2);
System.out.println("\n********JSON字符串 转 JAVA对象********");
User jp_user1=JSON.parseObject(str2,User.class);
System.out.println("JSON.parseObject(str2,User.class)==>"+jp_user1);
System.out.println("\n********JAVA对象 转 JSON对象********");
JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user2);
System.out.println("(JSONObject) JSON.toJSON(user2)==>"+jsonObject1.getString("name"));
System.out.println("\n********JSON对象 转 JAVA对象********");
User to_java_user = JSON.toJavaObject(jsonObject1,User.class);
System.out.println("JSON.toJavaObject(jsonObject1,User.class)==>"+to_java_user);
return "HELLO";
}
}