Original: HTTP://IWANTMOON.COM/POST/F94E49CAF9B6455DB7158474BAB4C4DD
Because of the job needs, now to do open platform, consider a variety of scenarios, basically ok down, Http Api and Web serivce two, REST Api will need to output JSON through spring MVC, there is actually some work to do, such as a large number of user access, how to guarantee performance, traffic and stability. Of course, we will discuss how to achieve it in this article.
Next, everybody follow me to do it.
First, we want to understand that spring MVC itself does not provide jsonview support, which we need to achieve through the help of Jackson's third party.
Then we're going to join the dependent reference:
<!--JSON - <Dependency> <groupId>Org.codehaus.jackson</groupId> <Artifactid>Jackson-core-lgpl</Artifactid> <version>1.8.1</version> </Dependency> <Dependency> <groupId>Org.codehaus.jackson</groupId> <Artifactid>Jackson-mapper-lgpl</Artifactid> <version>1.8.1</version> </Dependency> <Dependency> <groupId>Com.fasterxml.jackson.core</groupId> <Artifactid>Jackson-core</Artifactid> <version>2.1.0</version> </Dependency> <Dependency> <groupId>Com.fasterxml.jackson.core</groupId> <Artifactid>Jackson-databind</Artifactid> <version>2.1.0</version> </Dependency> <Dependency> <groupId>Com.fasterxml.jackson.core</groupId> <Artifactid>Jackson-annotations</Artifactid> <version>2.1.0</version> </Dependency>
It's part of the Jackson section, and there's spring MVC and the rest of it.
<Dependency> <groupId>Org.springframework</groupId> <Artifactid>Spring-webmvc</Artifactid> <version>${spring.version}</version> </Dependency> <Dependency> <groupId>Org.springframework</groupId> <Artifactid>Spring-context-support</Artifactid> <version>${spring.version}</version> </Dependency> <Dependency> <groupId>Javax.servlet</groupId> <Artifactid>Javax.servlet-api</Artifactid> <version>${servlet.version}</version> <Scope>Provided</Scope> </Dependency> <Dependency> <groupId>Javax.servlet</groupId> <Artifactid>Jstl</Artifactid> <version>${jstl.version}</version> </Dependency> <Dependency> <groupId>javax.servlet.jsp</groupId> <Artifactid>Jsp-api</Artifactid> <version>${jsp.version}</version> <Scope>Provided</Scope> </Dependency>
MVN clean Install
After a period of waiting, the dependency package is downloaded locally, OK, we can start configuring our project.
Since the center of this article is Spring MVC output JSON, it will skip the spring MVC configuration, if necessary, you can refer to Spring.io
Below, we add the Jaskson configuration:
<BeanID= "Stringconverter"class= "Org.springframework.http.converter.StringHttpMessageConverter"> < Propertyname= "Supportedmediatypes"> <List> <value>Text/plain;charset=utf-8</value> </List> </ Property> </Bean> <!--Output Object goto JSON support - <BeanID= "Jsonconverter"class= "Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></Bean> <Beanclass= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> < Propertyname= "Messageconverters"> <List> <refBean= "Stringconverter"/> <refBean= "Jsonconverter" /> </List> </ Property> </Bean>
Write a sample Controller:
@RestController Public class extends Basecontroller { = "/user", method = requestmethod.get) @JsonView (User.withoutpasswordview. class Public User getUser () {returnnew User ("Eric", "7!) Jd#h23 ");} }
Public classUser { Public InterfaceWithoutpasswordview {}; Public InterfaceWithpasswordviewextendsWithoutpasswordview {}; PrivateString username; PrivateString password; PublicUser () {} PublicUser (string Username, string password) { This. Username =username; This. Password =password; } @JsonView (Withoutpasswordview.class) PublicString GetUserName () {return This. Username; } @JsonView (Withpasswordview.class) PublicString GetPassword () {return This. Password; }}
OK, here is the result of the operation:
We have a problem, welcome to join us. Knock Code (http://iwantmoon.com) QQ Group: 299388502
Spring MVC output JSON (iwantmoon.com)