Spring 4.x let HTTP request return JSON string

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.