Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How springmvc returns json data in response to ajax data requests

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "springmvc how to respond to ajax data request to return json data", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "springmvc how to respond to ajax data request to return json data" bar!

1. Adopt the method of combining fastjson with springmvc

1. Introduce dependency

5.0.2.RELEASE UTF-8 1.8 org.springframework spring-context ${spring.version} org.springframework spring-web ${spring.version} org.springframework spring-webmvc ${spring.version} javax.servlet servlet-api 2.5 Provided javax.servlet.jsp jsp-api 2.0 provided org.springframework spring-core ${spring.version} com.fasterxml.jackson.core jackson-databind 2.9.0 com.alibaba fastjson 1.2.47 com.fasterxml.jackson. Core jackson-core 2.9.0 com.fasterxml.jackson.core jackson-annotations 2.9.0 com.fasterxml.jackson.dataformat jackson-dataformat-xml 2.9.8 junit junit 4.10

2. Write entity classes

Private String name;private int age;private String password;@JSONField (format = "yyyy-MM-dd") @ DateTimeFormat (pattern = "yyyy-MM-dd") @ JsonFormat (pattern = "yyyy-MM-dd") private Date date

3. Write controller control code

/ * return java object * @ param student * @ return * / @ RequestMapping ("/ testAjax") @ ResponseBodypublic Student testAjax (Student student) {System.out.println ("testAjax is working"); student.setName ("tom"); student.setAge (23); student.setPassword ("123456"); student.setDate (new Date ()); System.out.println (student); return student;}

4. Write the parsing code of configuration json in springmvc

Application/json;charset=UTF-8

5. Send ajax request data in response.jsp

$.getJSON ("/ user/testAjax", function (data) {console.log (data);})

Test the code:

Second, adopt the method of jackjson

1. Import dependencies are the same as above

2. Create an entity class

That is, the problem of encapsulating objects when the form was submitted in springmvc, encountered the first solution to the problem of date type solution 404

@ DateTimeFormat (pattern = "yyyy-MM-dd")

Private Date brithday

Private String name;private int age;private String password;@JSONField (format = "yyyy-MM-dd") @ DateTimeFormat (pattern = "yyyy-MM-dd") @ JsonFormat (pattern = "yyyy-MM-dd") private Date date

3. Write test code

@ Testpublic void test1 () throws Exception {Man man = new Man (); man.setName ("Chen Duoduo"); man.setAddress ("Chongqing Hechuan"); man.setBrithday (new Date ()); Man man1 = new Man (); man.setName ("Chen Duoduo"); man.setAddress ("Chongqing Hechuan"); man.setBrithday (new Date ()); Man man2 = new Man (); man.setName ("Chen Duoduo") Man.setAddress ("Chongqing Hechuan"); man.setBrithday (new Date ()); / / create the list collection to json List ps = new ArrayList (); ps.add (man); ps.add (man1); ObjectMapper objectMapper = new ObjectMapper (); String value = objectMapper.writeValueAsString (ps); System.out.println (value);} @ Testpublic void test3 () throws Exception {Map map = new HashMap () Map.put ("name", "Chen Duoduo"); map.put ("age", 23); map.put ("address", "Chongqing Hechuan"); ObjectMapper objectMapper = new ObjectMapper (); String value = objectMapper.writeValueAsString (map); System.out.println (value);}

}

The test results are as follows:

The list collection is converted into an array object, and the map combination is transformed into the same as the java object.

Thank you for your reading, the above is the content of "how springmvc responds to ajax data requests to return json data". After the study of this article, I believe you have a deeper understanding of how springmvc responds to ajax data requests to return json data, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report