forked from wuyouzhuguli/SpringAll
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c6e3c3
commit 2b182c4
Showing
9 changed files
with
341 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
17 changes: 17 additions & 0 deletions
17
18.Spring-Boot-Jackson/src/main/java/com/example/DemoApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
18.Spring-Boot-Jackson/src/main/java/com/example/config/JacksonConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
18.Spring-Boot-Jackson/src/main/java/com/example/config/UserDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
18.Spring-Boot-Jackson/src/main/java/com/example/config/UserSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
18.Spring-Boot-Jackson/src/main/java/com/example/controller/TestJsonController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
18.Spring-Boot-Jackson/src/main/java/com/example/pojo/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
Empty file.
16 changes: 16 additions & 0 deletions
16
18.Spring-Boot-Jackson/src/test/java/com/example/demo/DemoApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() { | ||
} | ||
|
||
} |