SpringBoot之WEB(六)
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;
}
}
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/
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}同上
运行效果
另外,也可以在使用图片作为启动图标,选择一个图片,放入resource目录,命名为banner.jpg即可
4. 源码下载
源码下载地址:https://download.csdn.net/download/huangjun0210/10775339