工具:Eclipse、jdk1.8、maven;
一:先建一个maven的项目
然后再pom文件中添加相关依赖和配置,具体如下:
<!--
spring boot 父节点依赖,引入这个之后相关的引入就不需要添加version配置,spring boot会自动选择最合适的版本进行添加。
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</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>
注意添加依赖后,记得update;
然后创建HelloController,code如下:
import java.util.Date;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello";
}
}
然后编码springboot启动类App.java
@SpringBootApplication
public class App// extends WebMvcConfigurerAdapter
{
public static void main( String[] args )
{
System.out.println( "--------------开始启动---------------!" );
SpringApplication.run(App.class, args);
System.out.println( "--------------启动成功---------------!" );
}
}
打开app.java,右键run as java application启动springboot应用;
--------------开始启动---------------!
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.1.RELEASE)
2019-05-27 15:27:52.942 INFO 2020 --- [ main] com.feiyun.springboot_hello.App : Starting App on TEST-PC with PID 2020 (E:\Eclise\MyWorkPlace\springboot-hello\target\classes started by Administrator in E:\Eclise\MyWorkPlace\springboot-hello)
2019-05-27 15:27:52.944 INFO 2020 --- [ main] com.feiyun.springboot_hello.App : No active profile set, falling back to default profiles: default
2019-05-27 15:27:52.985 INFO 2020 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4b0b0854: startup date [Mon May 27 15:27:52 CST 2019]; root of context hierarchy
2019-05-27 15:27:54.084 INFO 2020 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2019-05-27 15:27:54.097 INFO 2020 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2019-05-27 15:27:54.099 INFO 2020 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.5
2019-05-27 15:27:54.180 INFO 2020 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-05-27 15:27:54.180 INFO 2020 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1198 ms
2019-05-27 15:27:54.327 INFO 2020 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2019-05-27 15:27:54.331 INFO 2020 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-05-27 15:27:54.331 INFO 2020 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-05-27 15:27:54.331 INFO 2020 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-05-27 15:27:54.332 INFO 2020 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2019-05-27 15:27:54.592 INFO 2020 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4b0b0854: startup date [Mon May 27 15:27:52 CST 2019]; root of context hierarchy
2019-05-27 15:27:54.647 INFO 2020 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.feiyun.springboot_hello.HelloController.hello()
2019-05-27 15:27:54.648 INFO 2020 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/getDemo]}" onto public com.feiyun.springboot_hello.Demo com.feiyun.springboot_hello.HelloController.getDemo()
2019-05-27 15:27:54.652 INFO 2020 --- [ 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)
2019-05-27 15:27:54.652 INFO 2020 --- [ 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)
2019-05-27 15:27:54.674 INFO 2020 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-05-27 15:27:54.674 INFO 2020 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-05-27 15:27:54.706 INFO 2020 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-05-27 15:27:54.825 INFO 2020 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2019-05-27 15:27:54.870 INFO 2020 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2019-05-27 15:27:54.873 INFO 2020 --- [ main] com.feiyun.springboot_hello.App : Started App in 2.258 seconds (JVM running for 2.533)
--------------启动成功---------------!
运行成功后,打开浏览器访问hello方法,注意默认端口是8080;
end;
hello