springboot 创建一个项目

1、创建一个maven webapp项目

 

2、修改pom.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.hy</groupId>
    <artifactId>springboot-1</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-1 Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <finalName>springboot-1</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin </artifactId>
            </plugin>
        </plugins>
    </build>
</project>

 

2、写一个启动类

package com.hy.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;

@SpringBootApplication
public class AppTest implements EmbeddedServletContainerCustomizer{
    public static void main(String[] args) {
        SpringApplication.run(AppTest.class, args);
    }

    public void customize(ConfigurableEmbeddedServletContainer arg0) {
        arg0.setContextPath("/springboot");
        arg0.setPort(8081);
    }
}

 

3、写一个test控制类

package com.hy.test;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
public class TestController {
    
    @RequestMapping("/test1/{name}")
    String index(@PathVariable String name) {
        return "my name is " + name;
    }
    
    @RequestMapping("/")
    String home() {
        return "hello world";
    }
    
}

 

注意:

这里修改springboot的项目名称和端口哈:通过实现EmbeddedServletContainerCustomizer接口

posted on 2017-03-25 23:52  _故乡的原风景  阅读(745)  评论(0编辑  收藏  举报