JSON

一、Jackson

1
2
3
4
5
6
7
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>

代码测试:

一、使用@ controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.saxon.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.saxon.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
public class MyController {
@PostMapping ("/hello")//详相当于servletmappering
@ResponseBody
public String hello (User user, Model model) throws JsonProcessingException {
ObjectMapper obj = new ObjectMapper ();
return obj.writeValueAsString (user);
}
}

@ResponseBody:跳过视图解析器,直接返回结果;

ObjectMapper:转换对象

二、使用@Restcontroller

1
2
3
4
5
6
7
8
9
10
@RestController
public class MyController {
@PostMapping ("/hello")//详相当于servletmappering

public String hello (User user, Model model) throws JsonProcessingException {
ObjectMapper obj = new ObjectMapper ();
return obj.writeValueAsString (user);
}
}

跳过视图解析器直接返回数据;这样的话就可以直接不用@responseBody:

二、关于json的乱码问题的解决:

方法一:

1
@RequestMapping(value = "/xx", produces = "application/json;charset=utf-8")

方法二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!--Jackson乱码解决-->
<mvc:annotation-driven>
<mvc:message-converters>
<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
public String hello (User user, Model model) throws JsonProcessingException {
ObjectMapper obj = new ObjectMapper ();
obj.configure (SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false);
obj.setDateFormat (new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"));
return obj.writeValueAsString (new Date ());
}

第一种方法:

使用原来的日期时间类来格式化我们的时间

1
2
3
public String hello (User user, Model model) throws JsonProcessingException {
return new ObjectMapper ().writeValueAsString (new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss").format(new Date()));
}

第二种方法:

配置我们的objectMapper;

1
2
3
4
5
6
public String hello (User user, Model model) throws JsonProcessingException {
ObjectMapper obj = new ObjectMapper ();
obj.configure (SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false);
obj.setDateFormat (new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"));
return obj.writeValueAsString (new Date ());
}