1. 整合 Fastjson

1.1 添加Fastjson依赖

<dependency>
   <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.13</version>
</dependency>

1.2 创建一个配置管理类 WebConfig ,如下:

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;

/**
 * Created by HuangJun
 * 11:45 2018/11/7
 */
@Configuration
public class WebConfig {
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();

        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);

        HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;

        return new HttpMessageConverters(converter);

    }
}

1.3 案例开发

创建一个实体类 User:

import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private String id;
    private String name;
    private Integer age;

    @JSONField(format="yyyy-MM-dd")
    private Date birthday;

    public User(String id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
}

创建控制器类 FastjsonController

import com.springboot.web.model.User;
import com.springboot.web.utils.UUIDUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * Created by HuangJun
 * 11:31 2018/11/7
 */
@RestController
public class FastJsonController {

    @RequestMapping("/fastjson")
    public User jsontest() {
        User user = new User(UUIDUtil.getUUID(), "harden", 23,new Date());
        return user;
    }
}

打开浏览器,访问 http://localhost:8080/test/fastjson,结果如下图:
08-SpringBoot之WEB(六)——其他

2. 生成UUID

import java.util.UUID;

public class UUIDUtil {
    public static String getUUID() {
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replace("-", "");
        return uuid;
    }
}

3. 修改启动Banner

首先我们在resource目录下面放入一个banner.txt文件,Spring Boot启动项目的时候就会优先启动这个文件中的内容。然后我们用在线生成字符图标工具
http://www.network-science.de/ascii/
http://patorjk.com/software/taag/
08-SpringBoot之WEB(六)——其他
Spring Boot提供了一个枚举类AnsiColor,这个类可以控制banner.txt中的字符颜色,而且非常容易使用。
比如我可以将字符设置成颜色:BRIGHT_YELLOW ,版本号颜色设置成:BRIGHT_BLUE

${AnsiColor.BRIGHT_YELLOW}
'##::::'##:'########:'##:::::::'##::::::::'#######::
 ##:::: ##: ##.....:: ##::::::: ##:::::::'##.... ##:
 ##:::: ##: ##::::::: ##::::::: ##::::::: ##:::: ##:
 #########: ######::: ##::::::: ##::::::: ##:::: ##:
 ##.... ##: ##...:::: ##::::::: ##::::::: ##:::: ##:
 ##:::: ##: ##::::::: ##::::::: ##::::::: ##:::: ##:
 ##:::: ##: ########: ########: ########:. #######::
..:::::..::........::........::........:::.......:::
${AnsiColor.BRIGHT_BLUE}
::: Project (version:${application.version}) :::              Spring-Boot ${spring-boot.version}

配置参考:

${application.version}                这个是MANIFEST.MF文件中的版本号  
${application.formatted-version} 这个是上面的的版本号前面加v后上括号  
${spring-boot.version}               这个是springboot的版本号  
${spring-boot.formatted-version}同上 

运行效果08-SpringBoot之WEB(六)——其他

另外,也可以在使用图片作为启动图标,选择一个图片,放入resource目录,命名为banner.jpg即可

4. 源码下载

源码下载地址:https://download.csdn.net/download/huangjun0210/10775339

相关文章:

  • 2021-10-20
  • 2022-12-23
  • 2022-12-23
  • 2021-08-01
  • 2022-02-26
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-04
  • 2022-12-23
  • 2021-07-20
  • 2021-05-04
  • 2022-01-15
  • 2021-09-17
相关资源
相似解决方案