In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the introduction of the json framework and the handling of the results returned by Jackson". In the daily operation, I believe that many people have doubts about the introduction of the json framework and the handling of the results returned by Jackson. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "the introduction of the json framework and the handling of the results returned by Jackson". Next, please follow the editor to study!
Introduction of json framework. Jackson returns result processing.
Introduces the use of common json frameworks and annotations, and customizes the structure and format of json returns.
1. Commonly used frameworks such as Ali fastjson, Google gson, etc.
JavaBean is serialized to Json. Performance: Jackson > FastJson > Gson > Json-lib with the same structure
Jackson, FastJson and Gson class libraries have their own advantages and specialties.
Space for time, time for space.
2. Jackson processing related automation
Specified field does not return: @ JsonIgnore
Specify date format: @ JsonFormat (pattern= "yyyy-MM-dd hh:mm:ss", locale= "zh", timezone= "GMT+8")
Empty field does not return: @ JsonInclude (Include.NON_NUll)
Specify alias: @ JsonProperty
The entity class code is as follows:
Public class rData {@ JsonIgnore private String code; @ JsonProperty (value = "agenum") private int age; @ JsonFormat (pattern= yyyy-MM-dd hh:mm:ss ", locale=" zh ", timezone=" GMT+8 ") private Date createDate; @ JsonInclude (JsonInclude.Include.NON_NULL) private String name; public String getName () {return name;} public void setName (String name) {this.name = name } public Date getCreateDate () {return createDate;} public void setCreateDate (Date createDate) {this.createDate = createDate;} public int getAge () {return age;} public void setAge (int age) {this.age = age;} public String getCode () {return code;} public void setCode (String code) {this.code = code } public rData (String code, int age) {this.code = code; this.age = age;} public rData (String code, int age, Date createDate, String name) {this.code = code; this.age = createDate; this.name = name;}}
Test the class code:
@ RestControllerpublic class HttpController {@ GetMapping ("/ testjson") public Object param6 () {return new rData ("jackson", 1 Date (), "lion");}}
Results:
{"createDate": "2018-09-18 09:36:31", "name": "lion", "agenum": 1}
Code is ignored, so it is not displayed; age is replaced by alias "agenum"
Using jackson to return json data 1, how SpringMVC returns json data
1.1.Add jar package
Com.fasterxml.jackson.core jackson-databind 2.9.5
1.2. configure spring files, add mvc namespaces and constraints, etc.
Xmlns:mvc= "http://www.springframework.org/schema/mvc" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
1.3. add @ ResponseBody to the method
@ ResponseBody2, example
2.1.Configuring pom.xml
Junit junit 4.11 test org.springframework spring-webmvc 5.0.8.RELEASE org.springframework spring-web 5.0.8.RELEASE org.springframework spring-core 5.0.8.RELEASE org.springframework spring-context 5.0.8.RELEASE org.springframework Spring-beans 5.0.8.RELEASE org.springframework spring-context-support 5.0.8.RELEASE org.springframework spring-expression 5.0.8.RELEASE aopalliance aopalliance 1.0 org.aspectj aspectjweaver 1.8.13 org.springframework spring-aspects 5.0 .8.RELEASE org.springframework spring-aop 5.0.8.RELEASE com.fasterxml.jackson.core jackson-databind 2.9.5
2.2.Configuring spring.xml
2.3.Config web.xml
Archetype Created Web Application springMvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring.xml springMvc / characterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 characterEncodingFilter / *
2.4.Adding User entity classes
Package com.fan.entity;import java.util.Arrays;import java.util.Date;public class User {private String username; private String password; private int [] box; private Date date; public String getUsername () {return username;} public void setUsername (String username) {this.username = username;} public String getPassword () {return password } public void setPassword (String password) {this.password = password;} public int [] getBox () {return box;} public void setBox (int [] box) {this.box = box;} public Date getDate () {return date;} public void setDate (Date date) {this.date = date } @ Override public String toString () {return "User {" + "username='" + username +'\'+ ", password='" + password +'\'+ ", box=" + Arrays.toString (box) + ", date=" + date +'}';}}
2.5, TestJsonController test class
@ Controllerpublic class TestJsonController {/ / http://localhost:8080/testjson1 @ RequestMapping ("/ testjson1") @ ResponseBody / / returns json string annotations public User test () {User user=new User (); user.setUsername ("malijuan"); user.setPassword ("123"); user.setBox (new int [] {1meme2line 3}); user.setDate (new Date ()); return user } / / http://localhost:8080/testjson2 @ RequestMapping ("/ testjson2") @ ResponseBody / / returns the json string comment public List test2 () {User user=new User (); List list=new ArrayList (); for (int iTuno Tipi)
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.