Spring Boot 的 Hello World

来源:互联网 发布:怎样查看淘宝账号等级 编辑:程序博客网 时间:2024/05/20 09:44

Spring Boot 的 Hello World 

最近微服务的概念在IT界炒的有点火,笔者按捺不住,也来凑凑热闹,储备技能,废话不多说,上教程,高手勿喷,纯粹学习入门随笔记录。
首先新建一个maven工程:

maven是一个好的工程构建工具,帮我们管理复杂的jar依赖关系,而Spring boot也充分利用maven的优点。
首先亮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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.1.RELEASE</version></parent><groupId>org.hxb</groupId><artifactId>Hello</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><!--web应用基本环境配置 --><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></project>



新建一个SampleController.java:
package org.hxb;import org.springframework.boot.test.SpringApplicationConfiguration;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controller@SpringApplicationConfigurationpublic class SampleController {@RequestMapping(value="/")@ResponseBodyString home(){return "hello world!";}}


启动类Application.java:
package org.hxb;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.annotation.ComponentScan;@ComponentScan(basePackages={"org.hxb"})@EnableAutoConfigurationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class,args);}}

application.properties:
server.compression.enabled=trueserver.compression.min-response-size=1server.port=8081
在打开cmd,进入项目根目录,执行命令:mvn spring-boot:run便可以启动。 在浏览器访问:http://localhost:8081/





1 0
原创粉丝点击