In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use Spring Boot to deal with JSON data". In daily operation, I believe many people have doubts about how to use Spring Boot to deal with JSON data. 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 "how to use Spring Boot to deal with JSON data". Next, please follow the editor to study!
Spring Boot processing JSON data
JSON is the mainstream front-end and back-end data transmission mode at present. When a Java object or List collection is returned in Controller, Spring Boot will automatically convert it into JSON data.
Spring Boot has built-in JSON parsing function, when you add the spring-boot-starter-web module in the project, you can see that the default includes Jackson parser, can also be replaced with other parsers such as Fastjson.
1. Edit the Book class {Integer id; String name; String author; Float price; (pattern =) Date bookPublicationDate;} 2. Edit BookController
When returning data, you need to use the @ ResponseBody annotation. If you use @ Controller and @ ResponseBody annotations frequently, you can use @ RestController combined annotations instead.
{() {Book book = Book (); book.setId (); book.setName (); book.setAuthor (); book.setPrice (); book.setBookPublicationDate (Date ()); book;}}
After running, access http://localhost:8080/book directly in the address bar, and you can see the returned JSON data.
3. Transform collection data
Add the getBooks () method to create a List collection that holds three books. The specific source codes are as follows:
() {List bookList = ArrayList (); Book book1 = Book (); book1.setId (); book1.setName (); book1.setAuthor (); book1.setPrice (); book1.setBookPublicationDate (Date ()); Book book2 = Book (); book2.setId (); book2.setName (); book2.setAuthor (); book2.setPrice (); book2.setBookPublicationDate (Date ()); Book book3 = Book () Book3.setId (); book3.setName (); book3.setAuthor (); book3.setPrice (); book3.setBookPublicationDate (Date ()); bookList.add (book1); bookList.add (book2); bookList.add (book3); bookList;}
After running, access the http://localhost:8080/getBooks directly in the address bar, and you can see that the getBooks () method creates multiple Book objects encapsulated in the List collection and returns the JSON data to the client.
4. Replace Converter 1) use Gson
Gson is an open source JSON parser for Google. When adding dependencies, you need to remove the default jackson, as shown below:
Org.springframework.boot spring-boot-starter-web com.fasterxml.jackson.core jackson-databind com.google.code.gson gson
During Gson conversion, if you need to format date data, you need to customize HttpMessageConverter, and then provide a GsonHttpMessageConverter, as shown below:
{{GsonHttpMessageConverter converter = GsonHttpMessageConverter (); GsonBuilder builder = GsonBuilder (); builder.setDateFormat (); builder.excludeFieldsWithModifiers (Modifier.PROTECTED); Gson gson = builder.create (); converter.setGson (gson); converter;}}
Modify the Book class as follows:
{Integer id; String name; String author; Float price; Date bookPublicationDate;}
After running, access http://localhost:8080/getBooks directly in the address bar, as shown in the figure:
2) use fastjson
Fastjson is Alibaba's open source JSON parser and currently the fastest JSON parser. After integration, you need to provide the corresponding HttpMessageConverter to use. Add dependencies, as shown below:
Org.springframework.boot spring-boot-starter-web com.fasterxml.jackson.core jackson-databind com.alibaba fastjson
Next, add the HttpMessageConverter of fastjson, as follows:
{{FastJsonHttpMessageConverter converter = FastJsonHttpMessageConverter (); FastJsonConfig config = FastJsonConfig (); config.setDateFormat (); config.setCharset (Charset.forName ()); config.setSerializerFeatures (SerializerFeature.WriteClassName, SerializerFeature.WriteMapNullValue, SerializerFeature.PrettyFormat, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty) Converter.setFastJsonConfig (config); converter;}}
Then configure a response code in application.properties, as shown below:
Spring.http.encoding.force-response=true
After running, access http://localhost:8080/getBooks directly in the address bar, as shown in the figure:
At this point, the study on "how to use Spring Boot to process JSON data" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.