前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自定义fastjson解析+热部署

自定义fastjson解析+热部署

作者头像
南风
发布2019-04-22 16:51:48
5440
发布2019-04-22 16:51:48
举报
文章被收录于专栏:Java大联盟

今天给大家分享一篇Demi的原创教程,非常感谢这位集美貌与才华于一身的程序猿小姐姐,此处掌声应该再热烈一些。

废话不多说,直接上干货。

自定义fastjson解析

1. 创建简单的Springboot进行测试,建立一个无骨架的maven文件(无骨架:一路next下去),在pom.xml中添加:

代码语言:javascript
复制
<!--父节点 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
    <!--spring-boot-starter-web: MVC,AOP的依赖包....-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2. 下载完成,咱们就可以去搭建工程了,当然为了测试,首先创建一个Controller,实例如下:

3.你会发现已经部署完成,直接点击运行,打开浏览器输入

http://localhost:8080

网页展示“hello world”表示搭建成功。

4.添加自定义fastjson解析json数据,pom.xml:

代码语言:javascript
复制
<dependency>
    <!--persistence-api: @Entity....-->
    <groupId>javax.persistence</groupId>
    <artifactId>persistence-api</artifactId>
    <version>1.0.2</version>
</dependency>

5.实体类:

代码语言:javascript
复制
@Entity
public class User {
    private int id;
    private String name;
    private int age;
    private Date creatTime;
    public User(int id, String name, int age, Date creatTime){
        //省略

}
   @Override
    public String toString() {
       //省略
    }
}

6.控制器:

代码语言:javascript
复制
@Controller
public class UserController {
    @RequestMapping("getData")
    @ResponseBody
    public User getData(){
        User user=new User(1,"张三",22,new Date());
        return user;
    }
}

7.启动类:

代码语言:javascript
复制
@SpringBootApplication
public class App {
    public static void main(String[] args){
        SpringApplication.run(App.class,args);
    }
}

8.项目结构如图:

9.运行App.java,打开浏览器输入:

http://localhost:8080/getData:

10.现在我们要将creatTime解析成“yyyy-MM-dd HH:mm:ss”格式,pom.xml:

代码语言:javascript
复制
<!--添加fastjson 依赖包.解析json数据-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.15</version>
</dependency>

11.第一种方式:使用@Bean注解。

代码语言:javascript
复制
@SpringBootApplication
public class App {
   @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
            // 1、需要先定义一个 convert 转换消息的对象;
            FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

            //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
            FastJsonConfig fastJsonConfig = new FastJsonConfig();
            fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

            //3、在convert中添加配置信息.
            fastConverter.setFastJsonConfig(fastJsonConfig);
            HttpMessageConverter<?> converter = fastConverter;
            return new HttpMessageConverters(converter);
        }
    public static void main(String[] args){
        SpringApplication.run(App.class,args);
    }

12.第二种方式:启动类继承WebMvcConfigurerAdapter类。

代码语言:javascript
复制
@SpringBootApplication
public class App extends WebMvcConfigurerAdapter {
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);

        // 1、需要先定义一个 convert 转换消息的对象;
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat
        );

        //3、在convert中添加配置信息.
        fastConverter.setFastJsonConfig(fastJsonConfig);
        //4、将convert添加到converters当中.
        converters.add(fastConverter);
    }

    public static void main(String[] args){
        SpringApplication.run(App.class,args);
    }

}

推荐第二种方式,因为如果碰到了乱码问题,第二种解决乱码更为便捷,添加如下代码即可:

代码语言:javascript
复制
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);

最后一定不要忘了在实体类中添加注解:

若不希望接口返回一个属性,则使用 @JSONField(serialize = false)。

热部署

在上述整个过程中,你会发现如果修改代码就需要重新运行App.java,这种方式在整个开发过程中是非常浪费时间的,所以这个时候就需要使用热部署来提高开发效率。

1.pom.xml:

代码语言:javascript
复制
<!-- 热部署,spring boot devtools 依赖包.底下还要构建节点 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
    <scope>true</scope>
</dependency>

2.构建节点:

代码语言:javascript
复制
<build>
    <!--添加Spring boot的插件-->
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
            </configuration>
        </plugin>
    </plugins>
</build>

3.Eclipse这样设置就OK了,但是IDEA还不行,这是因为当我们修改了代码之后,IDEA默认是不会进行自动编译的,而spring-boot-devtools又是监测到classpath下的文件发生变化才会重启应用,所以需要设置IDEA的自动编译:

1.CTRL+SHIFT+A-->查找make project automatically--> 选中。

2.CTRL+SHIFT+A-->查找Registry-->

compiler.automake.allow.when.app.running。

3.指定文件进行热部署。

源码:

github

https://github.com/southwind9801/springboot_hibernate01.git

https://github.com/southwind9801/springboot_mybais02.git

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-04-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Java大联盟 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档