spring boot 使用demo

来源:互联网 发布:百度云 知乎 编辑:程序博客网 时间:2024/06/05 08:50

官方指引

一. Spring boot maven配置及使用

<parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.5.2.RELEASE</version>    <relativePath/> <!-- lookup parent from repository --></parent>

1. 需要使用某一个模块,引入就行,比如web

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId></dependency>

2. 新建配置文件和程序入口类

通用配置项 application.yml

server:  port: 8080  address: 0.0.0.0  compression:  enabled: true  connection-timeout: 5000  context-path: /  tomcat:    accept-count: 5000    max-connections: 2000    max-threads: 2000    min-spare-threads: 100    uri-encoding: UTF-8spring:    http:      encoding:        force: truelogging:  file: xxx.log  level:    root: info

程序入口类和demo

package hello;import org.springframework.boot.*;import org.springframework.boot.autoconfigure.*;import org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;@RestController@SpringBootApplicationpublic class SpringApplication {    @RequestMapping("/")    @ResponseBody    String home() {        return "Hello World!";    }    public static void main(String[] args) throws Exception {         //启动        SpringApplication.run(SampleController.class, args);    }}

然后输入http://localhost:8080

二. 日期转换

/** * 日期转换 * @param binder */@InitBinderprotected void initBinder(WebDataBinder binder) {    SimpleDateFormat dateFormat = new SimpleDateFormat(DateUtil.YYYY_MM_DD_HH_MM_SS);    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));}