美文网首页
3.springboot:完美使用FastJson解析xml数据

3.springboot:完美使用FastJson解析xml数据

作者: AiPuff | 来源:发表于2017-02-10 15:48 被阅读2457次

1.引入fastjson解析的依赖库

<!-- 引入fastjson解析的依赖库 -->
      <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>fastjson</artifactId>
             <version>1.2.15</version>
      </dependency>

2.配置fastjion:两个方法:

 方法一:(1)启动类继承extends WebMvcConfigurerAdapter
                   (2)覆盖方法configureMessageConverters

代码如下:

public class App extends WebMvcConfigurerAdapter
{
    @Override
    public void configureMessageConverters(
            List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        //下面是重写的代码
        
        //先定义一个convert转换消息的对象
        FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
        //添加fastJson的配置信息,比如,是否需要格式化返回json数据
        FastJsonConfig fastJsonConfig=new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //在convert中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        //将convert添加到converters中
        converters.add(fastConverter);
    }
    public static void main( String[] args )
    {
        /**
         * 在Main方法中启动我们的应用程序
         */
        System.out.println( "Hello World!" );
        SpringApplication.run(App.class, args);
       
    }
}

方法二:使用bean注入:

/**
     * 在这里我们使用@Bean注入fastJsonHttpMessageConvert
     * @return
     */
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters(){
        //先定义一个convert转换消息的对象
        FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
        //添加fastJson的配置信息,比如,是否需要格式化返回json数据
        FastJsonConfig fastJsonConfig=new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //在convert中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        HttpMessageConverter<?> converter=fastConverter;
        return new HttpMessageConverters(converter);
    }

3.测试:

Demo.class

    package com.zhaolixiang.spring_boot1;

import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;


/**
 * 测试实体类
 * @author hasee
 *
 */
public class Demo {
    private int id;
    private String name;
    //创建时间
    @JSONField(format="yyyy-MM-dd HH:mm")
    private Date createTime;
    
    /**
     * 现在不想显示备注信息
     * serialize:是否需要序列号属性
     */
    //备注信息
    @JSONField(serialize=false)
    private String remarks;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    public String getRemarks() {
        return remarks;
    }
    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }
    
    
}

调用:

/**
     * Spring Boot默认使用的json解析框架是jackson,
     * 所以springboot底层帮我们把对象返回来一个json数据
     */
    @RequestMapping("/demo")
    public Demo getDemo(){
        Demo demo=new Demo();
        demo.setId(110);
        demo.setName("赵理想");
        demo.setCreateTime(new Date());
        demo.setRemarks("这是备注信息");
        return demo;
    }

相关文章

网友评论

      本文标题:3.springboot:完美使用FastJson解析xml数据

      本文链接:https://www.haomeiwen.com/subject/pwbgittx.html