Skip to content

Commit 76d38f7

Browse files
committed
updating readme
making threadlocal static final
1 parent eecc22a commit 76d38f7

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
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.
44

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.
66

77
## Use cases
88

@@ -27,7 +27,7 @@ If you were to return a list of `MyObject`, you may not want to show the `contai
2727

2828
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`?
2929

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`):
3131

3232
```java
3333
import static com.monitorjbl.json.Match.match;
@@ -173,4 +173,15 @@ public class Context extends WebMvcConfigurerAdapter {
173173
}
174174
```
175175

176+
## Design
176177

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) class interprets 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) class that 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.

src/main/java/com/monitorjbl/json/JsonView.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
* Allows runtime alteration of JSON responses
88
*/
99
public class JsonView {
10-
private static ThreadLocal<JsonView> current;
10+
private static final ThreadLocal<JsonView> current = new ThreadLocal<>();
1111

1212
private final Object value;
1313
private final Map<Class<?>, Match> matches = new HashMap<>();
1414

1515
private JsonView(Object value) {
1616
this.value = value;
17-
current = new ThreadLocal<>();
1817
current.set(this);
1918
}
2019

0 commit comments

Comments
 (0)