The company's small project just started. I wrote some util methods.
The company uses Google's JSON technology to process JSON, which is transformed into entities by receiving httpservletrequest input streams.
Pom.xml as follows
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>
Here's how:
public static <T> T Convertjson (httpservletrequest request,type typeoft,string[] ignorefields) throws IOException , jsonsyntaxexception{
HttpServletRequest temp_request = Request;
String json = commonutil.getrequeststring (temp_request); Using the Request.getreader () method to get the string method is not posted here.
if (Json.equals ("")) {
throw new Nojsondataexception ();
}
A collection of fields that need to be ignored
list<string> list = new arraylist<> ();
for (String str:ignorefields) {
List.add (str);
}
Set the time format
Gson Gson = new Gsonbuilder (). Setdateformat ("Yyyy-mm-dd HH:mm:ss")
. Setexclusionstrategies (New Exclusionstrategy () {
Field names that need to be ignored
@Override
public boolean Shouldskipfield (FieldAttributes f) {
TODO auto-generated Method Stub
Return List.contains (F.getname ());
}
field types that need to be emptied
@Override
public boolean shouldskipclass (class<?> clazz) {
TODO auto-generated Method Stub
Return Clazz.getname (). Equals (Date.class.getName ());
}
}). Create ();
Return (T) Gson.fromjson (JSON, typeoft);
}
Google JSON technology-converting to JSON ignores certain attributes