Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd1a24bf

Browse files
Json example
1 parent9501ae3 commitd1a24bf

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
packagesporadic.jsonExample;
2+
3+
importorg.codehaus.jackson.annotate.JsonIgnore;
4+
importorg.codehaus.jackson.annotate.JsonIgnoreProperties;
5+
importorg.codehaus.jackson.annotate.JsonProperty;
6+
importorg.springframework.stereotype.Repository;
7+
importorg.springframework.stereotype.Service;
8+
9+
importlombok.Getter;
10+
importlombok.NoArgsConstructor;
11+
importlombok.Setter;
12+
importlombok.ToString;
13+
14+
/**This package is to demo how @JsonIgnore and @JsonIgnorProperties and @JsonProperty work:
15+
* @JsonIgnore is to annotate a field or method to make it free from being seriazlied by json
16+
* @JsonIgnoreProperties is to annotate a class, and you must/may specifiy which properties you want json to skip serializing.
17+
* @JsonProperty is to annotate a field and give this field a specific name when Jackson does serialization, e.g. a name.*/
18+
19+
20+
@NoArgsConstructor
21+
@Service
22+
@ToString
23+
@JsonIgnoreProperties({"noInterestingMember","forgetThisField"})
24+
publicclassJsonDemoClass {
25+
@Getter
26+
@Setter
27+
@JsonProperty("ID")
28+
publiclongid;
29+
30+
@Getter
31+
@Setter
32+
@JsonProperty("NAME")
33+
publicStringname;
34+
35+
@Getter
36+
@Setter
37+
publicStringnoInterestingMember;
38+
39+
@Getter
40+
@Setter
41+
@JsonIgnore
42+
@JsonProperty("ANOTHER-MEMEBR")
43+
publicintanotherMember;
44+
45+
@Getter
46+
@Setter
47+
publicdoubleforgetThisField;
48+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
packagesporadic.jsonExample;
2+
3+
importjava.io.IOException;
4+
5+
importorg.codehaus.jackson.map.ObjectMapper;
6+
importorg.springframework.context.support.AbstractApplicationContext;
7+
importorg.springframework.context.support.ClassPathXmlApplicationContext;
8+
9+
publicclassJsonIgnoreAnnotationDemo {
10+
11+
publicstaticvoidmain(String...args) {
12+
13+
AbstractApplicationContextcontext =newClassPathXmlApplicationContext(
14+
"spring-configuration/json-spring-configuration.xml");
15+
JsonDemoClassjsonDemoClass = (JsonDemoClass)context.getBean("jsonDemoClass");
16+
17+
ObjectMappermapper =newObjectMapper();
18+
19+
jsonDemoClass.setId(1);
20+
jsonDemoClass.setName("Test program!");
21+
jsonDemoClass.setAnotherMember(123);
22+
jsonDemoClass.setForgetThisField(123.456);
23+
jsonDemoClass.setNoInterestingMember("Something not interesting!");
24+
25+
Strings =null;
26+
try {
27+
s =mapper.writeValueAsString(jsonDemoClass);
28+
}catch (IOExceptione) {
29+
e.printStackTrace();
30+
}
31+
System.out.println("String s is: " +s);
32+
33+
JsonDemoClassjsonDemoClass2 = (JsonDemoClass)context.getBean("jsonDemoClass");
34+
try {
35+
jsonDemoClass2 =mapper.readValue(s,JsonDemoClass.class);
36+
}catch (IOExceptione) {
37+
e.printStackTrace();
38+
}
39+
System.out.println("jsonDemoClass2.toString() s is: " +jsonDemoClass2.toString() +"\nThe end.");
40+
}
41+
42+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<beansxmlns="http://www.springframework.org/schema/beans"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:context="http://www.springframework.org/schema/context"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans
7+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
8+
http://www.springframework.org/schema/context
9+
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
10+
11+
<context:component-scanbase-package="sporadic.JsonExample"></context:component-scan>
12+
13+
</beans>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp