springboot[入门笔记]

来源:互联网 发布:外国人中国菜知乎 编辑:程序博客网 时间:2024/06/04 01:05

工具:
myeclipse
maven

  1. 新建一个maven工程.
  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>springbooth.test</groupId>    <artifactId>springbooth</artifactId>    <packaging>jar</packaging>    <version>0.0.1-SNAPSHOT</version>    <name>springbooth Maven Webapp</name>    <url>http://maven.apache.org</url>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.4.0.RELEASE</version>    </parent>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>    </dependencies>    <build>        <finalName>springbooth</finalName>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build>    <repositories>        <repository>            <id>spring-snapshots</id>            <url>http://repo.spring.io/snapshot</url>            <snapshots>                <enabled>true</enabled>            </snapshots>        </repository>        <repository>            <id>spring-milestones</id>            <url>http://repo.spring.io/milestone</url>        </repository>    </repositories>    <pluginRepositories>        <pluginRepository>            <id>spring-snapshots</id>            <url>http://repo.spring.io/snapshot</url>        </pluginRepository>        <pluginRepository>            <id>spring-milestones</id>            <url>http://repo.spring.io/milestone</url>        </pluginRepository>    </pluginRepositories></project>

3.新建测试Controller

package com.main.sy;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@EnableAutoConfigurationpublic class Example {    @RequestMapping("/")    String home() {        return "Hello World2!";    }    @RequestMapping("/hello/{myName}")    String index(@PathVariable String myName) {        return "Hello " + myName + "!!!";    }}

4.新建Application.java

package com.main;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}
  1. 项目右键-> Run As ->Java Application >选择main入口.
  2. 浏览器访问: localhost:8080.
  3. 更改自带Tomcat配置端口.
  4. 在resources文件夹下新建application.properties
server.port=8011

重启生效.

(如有需要Controller跳转jsp页面, 需要用war或者容器部署. 采用自带Tomcat不做视图解析.)

  1. 用开发工具导出jar可以直接运行.