diff --git a/18.Spring-Boot-Jackson/pom.xml b/18.Spring-Boot-Jackson/pom.xml new file mode 100644 index 0000000..190b9f4 --- /dev/null +++ b/18.Spring-Boot-Jackson/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + com.example + Spring-Boot-Jackson + 0.0.1-SNAPSHOT + jar + + demo + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 1.5.9.RELEASE + + + + + UTF-8 + UTF-8 + 1.7 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/18.Spring-Boot-Jackson/src/main/java/com/example/DemoApplication.java b/18.Spring-Boot-Jackson/src/main/java/com/example/DemoApplication.java new file mode 100644 index 0000000..12ae367 --- /dev/null +++ b/18.Spring-Boot-Jackson/src/main/java/com/example/DemoApplication.java @@ -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); + + } +} diff --git a/18.Spring-Boot-Jackson/src/main/java/com/example/config/JacksonConfig.java b/18.Spring-Boot-Jackson/src/main/java/com/example/config/JacksonConfig.java new file mode 100644 index 0000000..a9eb4a3 --- /dev/null +++ b/18.Spring-Boot-Jackson/src/main/java/com/example/config/JacksonConfig.java @@ -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; + } +} diff --git a/18.Spring-Boot-Jackson/src/main/java/com/example/config/UserDeserializer.java b/18.Spring-Boot-Jackson/src/main/java/com/example/config/UserDeserializer.java new file mode 100644 index 0000000..afe6e62 --- /dev/null +++ b/18.Spring-Boot-Jackson/src/main/java/com/example/config/UserDeserializer.java @@ -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 { + + @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; + } +} diff --git a/18.Spring-Boot-Jackson/src/main/java/com/example/config/UserSerializer.java b/18.Spring-Boot-Jackson/src/main/java/com/example/config/UserSerializer.java new file mode 100644 index 0000000..0a9cbb2 --- /dev/null +++ b/18.Spring-Boot-Jackson/src/main/java/com/example/config/UserSerializer.java @@ -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 { + + @Override + public void serialize(User user, JsonGenerator generator, SerializerProvider provider) + throws IOException, JsonProcessingException { + generator.writeStartObject(); + generator.writeStringField("user-name", user.getUserName()); + generator.writeEndObject(); + } +} diff --git a/18.Spring-Boot-Jackson/src/main/java/com/example/controller/TestJsonController.java b/18.Spring-Boot-Jackson/src/main/java/com/example/controller/TestJsonController.java new file mode 100644 index 0000000..14440f2 --- /dev/null +++ b/18.Spring-Boot-Jackson/src/main/java/com/example/controller/TestJsonController.java @@ -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 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 list = mapper.readValue(jsonStr, type); + String msg = ""; + for (User user : list) { + msg += user.getUserName(); + } + return msg; + } +} diff --git a/18.Spring-Boot-Jackson/src/main/java/com/example/pojo/User.java b/18.Spring-Boot-Jackson/src/main/java/com/example/pojo/User.java new file mode 100644 index 0000000..a7b6795 --- /dev/null +++ b/18.Spring-Boot-Jackson/src/main/java/com/example/pojo/User.java @@ -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; + } + +} diff --git a/18.Spring-Boot-Jackson/src/main/resources/application.properties b/18.Spring-Boot-Jackson/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/18.Spring-Boot-Jackson/src/test/java/com/example/demo/DemoApplicationTests.java b/18.Spring-Boot-Jackson/src/test/java/com/example/demo/DemoApplicationTests.java new file mode 100644 index 0000000..b76e7f2 --- /dev/null +++ b/18.Spring-Boot-Jackson/src/test/java/com/example/demo/DemoApplicationTests.java @@ -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() { + } + +}