You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+13-2Lines changed: 13 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
Ever needed to programmatically include or exclude a field from your Spring MVC response data? Well, if you have then you probably know by now that it's very difficult to do. Spring is by nature very declarative (annotations for everything!), so doing something programmatically gets ugly fast.
4
4
5
-
While the declarative style certainly has many benefits (compile-time checking, ease of refactoring, etc.), the inability to simply and programmatically control your responses is one major downside. Inspired by [VRaptor](http://www.vraptor.org/), this plugin provides an easy way to alter the JSON output on the fly.
5
+
While the declarative style certainly has many benefits (compile-time checking, ease of refactoring, etc.), the inability to simply and programmatically control your responses is one major downside. Inspired by [VRaptor](http://www.vraptor.org/), this library provides an easy way to alter the JSON output on the fly.
6
6
7
7
## Use cases
8
8
@@ -27,7 +27,7 @@ If you were to return a list of `MyObject`, you may not want to show the `contai
27
27
28
28
The typically suggested pattern suggests using the `@JsonIgnore` annotation on the field. However, this effectively makes this field permanently ignored everywhere in your app. What if you want only don't want to show this field when dealing with a single instance rather than a `List`?
29
29
30
-
Using `JsonView` allows you to filter this field out quickly and easily in your controller methods:
30
+
Using `JsonView` allows you to filter this field out quickly and easily in your controller methods (note that your method return value must be `void`):
31
31
32
32
```java
33
33
import staticcom.monitorjbl.json.Match.match;
@@ -173,4 +173,15 @@ public class Context extends WebMvcConfigurerAdapter {
173
173
}
174
174
```
175
175
176
+
## Design
176
177
178
+
Basic design information about this library.
179
+
180
+
#### Serializer
181
+
As stated above, the heart of this library is the custom Jackson serializer. The [JsonViewSerializer](src/main/java/com/monitorjbl/json/JsonViewSerializer.java) classinterprets both the object to serialize and the include/exclude config to write your object as a String. Pretty much everything else is simply to integrate it nicely with Spring MVC.
182
+
183
+
#### Spring MVC Integration
184
+
The [JsonView](src/main/java/com/monitorjbl/json/JsonView.java) classthat you refer to stores a `ThreadLocal` var containing your returned object and all configuration information. The `ThreadLocal` is used to store the result so your method doesn't have to return a particular type value. If you have used `JsonView` at all in your current thread, this value will be set and your response will generated from it.
185
+
186
+
#### External use information
187
+
Use of the JsonViewSerializer outside of the Spring MVC integration is fine, however there will still be a `ThreadLocal` reference to your object and config. If this cause an issue for you, you can simply call `JsonView.with()` again to reset the reference.
0 commit comments