SpringMVC4-JSON2

nexthexonextbutterflyvolantisyearnyiliashokaindigoapollolandscapecactusmateryicarusfluidmaterial

json用起来

  • 导入json
  • 配置json
  • 新建对象
  • 转化为json
1
2
3
4
5
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.2</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!--解决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>
1
2
3
4
5
6
@ResponseBody
@RequestMapping("/json1")
public String json1() throws JsonProcessingException {
return new ObjectMapper().writeValueAsString(
new node(1, 2L, "hello"));
}

fastjson用起来

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
1
2
3
4
5
6
7

@ResponseBody
@RequestMapping("/json2")
public String json2() {
return JSON.toJSONString(new node(1, 2L, "hello fastjson 你好"));
}

感谢您的阅读。 🙏 关于转载请看这里