Springboot 入门之Hello World
首先使用maven进行包加载和配置,但是你maven一定要配置好,maven的setting.xml文件一定要配置好,不然jar包加载不了的。
<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.esoft.jdp2p</groupId> <artifactId>springboot</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springboot</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> </parent> <distributionManagement> <repository> <id>jinding-releases</id> <name>Jinding Release Repository</name> <url>http://server:8080/nexus-2.9.0-04/content/repositories/releases/</url> </repository> <snapshotRepository> <id>jinding-snapshots</id> <name>Jinding Snapshot Repository</name> <url>http://server:8080/nexus-2.9.0-04/content/repositories/snapshots/</url> </snapshotRepository> </distributionManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <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>
确认项目的配置正确,我用的是jdk1.7
下面是hello world 程序!但是类名是按照官网上叫的。
package com.lee.controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @EnableAutoConfiguration public class SampleController { @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(SampleController.class, args); } }
运行时,没有把整个项目都加载到servers的tomcat下,只要右击java类,出现Run as 就可以了!
端口默认用的是8080,此时用浏览器访问http://localhost:8080/即可