Many applications have already started to return the response as JSON strings, so a service-side program developed based on the Springframework framework makes it a common tool to return JSON strings in response.
Here's how to quickly and easily return a JSON string under the SPRING-MVC framework.
First, the @reponsebody annotation needs to be added to the Controller class's method name header, but it is not enough to add this annotation, and an error in HTTP status 406 occurs when you use httpclient4.5.x to simulate a POST request:
Java code Copy Code collection code 1.406 1. 1 new)
If you use spring-MVC Framework Development, then the solution is as follows:1) in spring-add Java code to copy Code collection code in Servlet.xml1.<mvc:annotation-driven>2. <mvc:message-converters register-defaults="true">3. <beanclass="Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" 4. p:supportedmediatypes="*/*"/>5. </mvc:message-converters>6.</mvc:annotation-driven>2) Maven project, add Java code to copy Code collection code in Pom.xml1.<dependency>2. <groupId>org.codehaus.jackson</groupId>3. <artifactId>jackson-mapper-asl</artifactId>4. <version>1.9. -</version>5.</dependency>6.<dependency>7. <groupId>org.codehaus.jackson</groupId>8. <artifactId>jackson-core-asl</artifactId>9. <version>1.9. -</version>Ten.</dependency>Gradle Project, add Java code to copy Code collection code under the Dependencies entry for the Build.gradle file1. Compile'org.codehaus.jackson:jackson-mapper-asl:1.9.13' 2. Compile'org.codehaus.jackson:jackson-core-asl:1.9.13'attached: I used httpclient4 during the test.5. x data returned when simulating a POST request Java code Copy Code collection code1. response.tostring ():2. httpresponseproxy{http/1.1 $OK [server:apache-coyote/1.1, content-type:application/json;charset=utf-8, transfer-encoding:chunked, Date:tue, inJul - ,: the: inGMT]} 3. 4. Entityutils.tostring (Entity,"Utf-8"): 5. {"Code":0,"Flag":false,"message":"user name is wrong, please fill in mobile phone number or email!","UID":-1,"Mobile":NULL,"Email":NULL,"username":NULL}
Spring 4.x let HTTP request return JSON string