Json create entity classes, as follows:
public class Demo { private int age; private String address; private String name; private String remark; private Date createTime; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } }
A: jackson return json data using, as follows:
Description: spring boot default json analytic framework jsckson resolution, there is no need to add any dependency;
code show as below:
@RestController public class HelloController { @RequestMapping("/hello") public String hello(){ return "hello"; } @RequestMapping("/getDemo") public Demo getDemo(){ Demo demo = new Demo(); demo.setAddress("谁登录看风景"); demo.setAge(11); demo.setCreateTime(new Date()); return demo; } }
direct interview:
Two: Use fastjson
You need to add a dependency:
<dependency> <groupId> com.alibaba </ groupId> <artifactId> FASTJSON </ artifactId> <Version> 1.2.15 </ Version> </ dependency> to say at this very important, said the official documents of 1 .2.10 later, there will be two ways to support HttpMessageconvert, one FastJsonHttpMessageConverter, supports the following version 4.2, supports more than one FastJsonHttpMessageConverter4 version 4.2,
specifically what is the difference being no in-depth study. Here it is to say: do not support the version of the low, so here is 1.2.10+ minimum requirements.
Configuration fastjon (supports two methods)
The first:
(1) Start WebMvcConfigurerAdapter class inherits the extends
(2) covering method configureMessageConverters
code show as below:
@SpringBootApplication public class App extends WebMvcConfigurerAdapter { public static void main( String[] args ) { System.out.println( "--------------开始启动---------------!" ); SpringApplication.run(App.class, args); System.out.println( "--------------启动成功---------------!" ); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { // TODO Auto-generated method stub super.configureMessageConverters(converters); FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures( SerializerFeature.PrettyFormat );
//处理中文乱码问题 List<MediaType> fastMediaTypes = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(fastMediaTypes); fastConverter.setFastJsonConfig(fastJsonConfig); converters.add(fastConverter); } }
Restart, then visit:
The second: In the startup class App.java injected Bean: HttpMessageConverters, code is as follows:
import java.util.ArrayList; import java.util.List; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.web.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; @SpringBootApplication public class App extends WebMvcConfigurerAdapter { public static void main( String[] args ) { System.out.println( "--------------开始启动---------------!" ); SpringApplication.run(App.class, args); System.out.println( "--------------启动成功---------------!" ); } @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //处理中文乱码问题 List<MediaType> fastMediaTypes = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(fastMediaTypes); fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); } }
Note: Do not guide the wrong package, is: import org.springframework.http.MediaType;
The results after the first visit and the same;
The first and second can be used, according to your own personal preferences;
After using fastjson framework, it may also be used to annotate fastjson json data returned special handling, as follows:
@JSONField (the format = "the MM-dd-YYYY HH: mm: SS" ) // Returns formatted date Private a Date createTime ; @JSONField (the serialize = to false) // whether the serialization attribute, if false, then the the data does not return information Private String Use the remark ;
Other characteristics of their own interest research;