创建一个最简单的springboot项目
废话不多说直接开整!!
打开eclipse新建一个maven项目,目录格式如下:
打开pom.xml文件,引入父依赖,web依赖,并设置JDK版本
<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.freetek</groupId>
<artifactId>nmsc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
保存之后,右键项目名->maven->update project
然后编写启动类,目录结构
App.java
package com.freetek.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
最后,写一个Contoller类
package com.freetek.springboot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Hello {
@RequestMapping(value = "/hello")
public String hello() {
return "hello spring boot";
}
}
@RestController :这个注解相当于@ResponseBody 和 @Controller两个注解的组合然后右键->run as -> java application
控制台打印如下内容表示启动成功:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.1.RELEASE)
2017-11-29 16:57:01.315 INFO 5888 --- [ main] com.freetek.springboot.App : Starting App on Freetek-013 with PID 5888 (E:\workspace\springboot\target\classes started by Jinqiang in E:\workspace\springboot)
2017-11-29 16:57:01.317 INFO 5888 --- [ main] com.freetek.springboot.App : No active profile set, falling back to default profiles: default
2017-11-29 16:57:01.363 INFO 5888 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5a63f509: startup date [Wed Nov 29 16:57:01 CST 2017]; root of context hierarchy
2017-11-29 16:57:02.732 INFO 5888 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-11-29 16:57:02.757 INFO 5888 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-11-29 16:57:02.758 INFO 5888 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.5
2017-11-29 16:57:02.877 INFO 5888 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-11-29 16:57:02.877 INFO 5888 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1517 ms
2017-11-29 16:57:03.112 INFO 5888 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-11-29 16:57:03.116 INFO 5888 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-11-29 16:57:03.117 INFO 5888 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-11-29 16:57:03.117 INFO 5888 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-11-29 16:57:03.117 INFO 5888 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-11-29 16:57:03.460 INFO 5888 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5a63f509: startup date [Wed Nov 29 16:57:01 CST 2017]; root of context hierarchy
2017-11-29 16:57:03.548 INFO 5888 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-11-29 16:57:03.549 INFO 5888 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-11-29 16:57:03.600 INFO 5888 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-11-29 16:57:03.600 INFO 5888 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-11-29 16:57:03.649 INFO 5888 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-11-29 16:57:03.828 INFO 5888 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-11-29 16:57:04.079 INFO 5888 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-11-29 16:57:04.084 INFO 5888 --- [ main] com.freetek.springboot.App : Started App in 3.092 seconds (JVM running for 3.404)
打开浏览器,访问:http://localhost:8080/hello
返回 hello spring boot 表示成功。
over