Spring Boot中的JSON技术
This commit is contained in:
parent
5c6e3c3f16
commit
2b182c4fc6
|
|
@ -0,0 +1,50 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>Spring-Boot-Jackson</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>demo</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.9.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.7</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package com.example;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
|
||||
public static void main(String[] args) throws JsonProcessingException, IOException {
|
||||
SpringApplication.run(DemoApplication.class, args);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package com.example.config;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@Configuration
|
||||
public class JacksonConfig {
|
||||
@Bean
|
||||
public ObjectMapper getObjectMapper(){
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
return mapper;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package com.example.config;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.example.pojo.User;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
public class UserDeserializer extends JsonDeserializer<User> {
|
||||
|
||||
@Override
|
||||
public User deserialize(JsonParser parser, DeserializationContext context)
|
||||
throws IOException, JsonProcessingException {
|
||||
JsonNode node = parser.getCodec().readTree(parser);
|
||||
String userName = node.get("user-name").asText();
|
||||
User user = new User();
|
||||
user.setUserName(userName);
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package com.example.config;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.example.pojo.User;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
|
||||
public class UserSerializer extends JsonSerializer<User> {
|
||||
|
||||
@Override
|
||||
public void serialize(User user, JsonGenerator generator, SerializerProvider provider)
|
||||
throws IOException, JsonProcessingException {
|
||||
generator.writeStartObject();
|
||||
generator.writeStringField("user-name", user.getUserName());
|
||||
generator.writeEndObject();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,118 @@
|
|||
package com.example.controller;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import com.example.pojo.User;
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@Controller
|
||||
public class TestJsonController {
|
||||
|
||||
@Autowired
|
||||
ObjectMapper mapper;
|
||||
|
||||
@JsonView(User.AllUserFieldView.class)
|
||||
@RequestMapping("getuser")
|
||||
@ResponseBody
|
||||
public User getUser() {
|
||||
User user = new User();
|
||||
user.setUserName("mrbird");
|
||||
user.setAge(26);
|
||||
user.setPassword("123456");
|
||||
user.setBirthday(new Date());
|
||||
return user;
|
||||
}
|
||||
|
||||
@RequestMapping("serialization")
|
||||
@ResponseBody
|
||||
public String serialization() {
|
||||
try {
|
||||
User user = new User();
|
||||
user.setUserName("mrbird");
|
||||
user.setBirthday(new Date());
|
||||
String str = mapper.writeValueAsString(user);
|
||||
return str;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@RequestMapping("readjsonstring")
|
||||
@ResponseBody
|
||||
public String readJsonString() {
|
||||
try {
|
||||
String json = "{\"name\":\"mrbird\",\"age\":26}";
|
||||
JsonNode node = this.mapper.readTree(json);
|
||||
String name = node.get("name").asText();
|
||||
int age = node.get("age").asInt();
|
||||
return name + " " + age;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@RequestMapping("readjsonasobject")
|
||||
@ResponseBody
|
||||
public String readJsonAsObject() {
|
||||
try {
|
||||
String json = "{\"user-name\":\"mrbird\"}";
|
||||
User user = mapper.readValue(json, User.class);
|
||||
String name = user.getUserName();
|
||||
return name;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@RequestMapping("formatobjecttojsonstring")
|
||||
@ResponseBody
|
||||
public String formatObjectToJsonString() {
|
||||
try {
|
||||
User user = new User();
|
||||
user.setUserName("mrbird");
|
||||
user.setAge(26);
|
||||
user.setPassword("123456");
|
||||
user.setBirthday(new Date());
|
||||
String jsonStr = mapper.writeValueAsString(user);
|
||||
return jsonStr;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@RequestMapping("updateuser")
|
||||
@ResponseBody
|
||||
public int updateUser(@RequestBody List<User> list) {
|
||||
return list.size();
|
||||
}
|
||||
|
||||
@RequestMapping("customize")
|
||||
@ResponseBody
|
||||
public String customize() throws JsonParseException, JsonMappingException, IOException {
|
||||
String jsonStr = "[{\"userName\":\"mrbird\",\"age\":26},{\"userName\":\"scott\",\"age\":27}]";
|
||||
JavaType type = mapper.getTypeFactory().constructParametricType(List.class, User.class);
|
||||
List<User> list = mapper.readValue(jsonStr, type);
|
||||
String msg = "";
|
||||
for (User user : list) {
|
||||
msg += user.getUserName();
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
package com.example.pojo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
import com.example.config.UserDeserializer;
|
||||
import com.example.config.UserSerializer;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
//@JsonIgnoreProperties({ "password", "age" })
|
||||
//@JsonNaming(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class)
|
||||
//@JsonSerialize(using = UserSerializer.class)
|
||||
//@JsonDeserialize (using = UserDeserializer.class)
|
||||
public class User implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 6222176558369919436L;
|
||||
|
||||
public interface UserNameView {
|
||||
};
|
||||
|
||||
public interface AllUserFieldView extends UserNameView {
|
||||
};
|
||||
|
||||
@JsonView(UserNameView.class)
|
||||
private String userName;
|
||||
|
||||
@JsonView(AllUserFieldView.class)
|
||||
private int age;
|
||||
|
||||
// @JsonIgnore
|
||||
@JsonView(AllUserFieldView.class)
|
||||
private String password;
|
||||
|
||||
// @JsonProperty("bth")
|
||||
// @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@JsonView(AllUserFieldView.class)
|
||||
private Date birthday;
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Date getBirthday() {
|
||||
return birthday;
|
||||
}
|
||||
|
||||
public void setBirthday(Date birthday) {
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
package com.example.demo;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class DemoApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue