SpringBoot实战:SpringBoot之Hello world

这里以最新的2.2.0.RELEASE版本为例子,别问我为啥,由于它是目前为止最新的一个版本。java

建立maven工程,引入依赖git

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wusy.demo</groupId>
    <artifactId>spring-boot-wusy-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 继承SpringBoot父包 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath></relativePath>
    </parent>

    <dependencies>
        <!-- spring boot web支持:mvc,aop... -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>
    
</project>

在resource下建立application.properties或者application.yml,SpringBoot会自动加载这个两个配置文件。web

#编码设置
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.force=true
spring.http.encoding.enabled=true
spring.messages.encoding=UTF-8

# tomcat 配置
server.tomcat.accept-count=1000
server.tomcat.max-threads=500
# session超时时间,单位是秒
server.servlet.session.timeout=1800

#当前应用http端口
server.port=8787
#访问地址,该配置能够不配置,则直接经过ip+port访问
#server.servlet.context-path=/demo

建立SpringBoot启动类spring

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author wusy
 * Company: xxxxxx科技有限公司
 * Createtime : 2020/2/24 21:42
 * Description : SpringBoot应用启动器
 */
@SpringBootApplication
public class Application {

    private static Logger logger = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.run(args);
        logger.info("启动成功");
    }
}

建立Controllerapache

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wusy
 * Company: xxxxxx科技有限公司
 * Createtime : 2020/2/24 21:54
 * Description :
 */
@RestController
@RequestMapping("/api/demo")
public class HelloWorldController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return "hello world";
    }
}

运行Application类api

打开浏览器,在地址栏输入http://127.0.0.1:8787/api/demo/hello浏览器

至此,SpringBoot简单的Hello world例子演示完毕。tomcat

项目码云地址:https://gitee.com/wusycode/spring-boot-wusy-demo.gitsession

相关文章
相关标签/搜索