你的位置:首页 > Java教程

[Java教程]SpringBoot入门

  1、创建一个Maven项目,添加一个parent,代码如下

 <parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.3.4.RELEASE</version>  </parent>

 

  2、添加一个依赖

 <dependencies>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-web</artifactId>     </dependency>     </dependencies>  

  3、配置java版本,若不填,默认1.6

 <build><!-- 配置java版本 不配置的话默认父类配置的是1.6-->  <pluginManagement>   <plugins>    <plugin>     <artifactId>maven-compiler-plugin</artifactId>     <configuration>      <source>1.7</source>      <target>1.7</target>     </configuration>    </plugin>   </plugins>  </pluginManagement> </build>

  4、新建一个java文件

  

package com.test;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @RestController 相当于@Controller和@RequestBody * */@RestController@EnableAutoConfigurationpublic class HelloController {    @RequestMapping("/hello")  public String hello(){    return "Hello Word SpringBoot";  }    public static void main(String[] args) {    SpringApplication.run(HelloController.class, args);  }}

  最后run运行

  

 .  ____     _      __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::    (v1.3.4.RELEASE)2017-08-12 13:51:00.098 INFO 2124 --- [      main] com.test.HelloController         : Starting HelloController on DESKTOP-M8DR5S3 with PID 2124 (F:\MyEclipse\mywork\spinrBoot-hello\target\classes started by 592302116 in F:\MyEclipse\mywork\spinrBoot-hello)2017-08-12 13:51:00.113 INFO 2124 --- [      main] com.test.HelloController         : No active profile set, falling back to default profiles: default2017-08-12 13:51:00.160 INFO 2124 --- [      main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5b0de4ee: startup date [Sat Aug 12 13:51:00 CST 2017]; root of context hierarchy2017-08-12 13:51:01.770 INFO 2124 --- [      main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)2017-08-12 13:51:01.815 INFO 2124 --- [      main] o.apache.catalina.core.StandardService  : Starting service Tomcat2017-08-12 13:51:01.815 INFO 2124 --- [      main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.332017-08-12 13:51:01.955 INFO 2124 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]    : Initializing Spring embedded WebApplicationContext2017-08-12 13:51:01.955 INFO 2124 --- [ost-startStop-1] o.s.web.context.ContextLoader      : Root WebApplicationContext: initialization completed in 1795 ms2017-08-12 13:51:02.299 INFO 2124 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean    : Mapping servlet: 'dispatcherServlet' to [/]2017-08-12 13:51:02.315 INFO 2124 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]2017-08-12 13:51:02.315 INFO 2124 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2017-08-12 13:51:02.315 INFO 2124 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]2017-08-12 13:51:02.315 INFO 2124 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]2017-08-12 13:51:02.565 INFO 2124 --- [      main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5b0de4ee: startup date [Sat Aug 12 13:51:00 CST 2017]; root of context hierarchy2017-08-12 13:51:02.630 INFO 2124 --- [      main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.test.HelloController.hello()2017-08-12 13:51:02.630 INFO 2124 --- [      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-08-12 13:51:02.630 INFO 2124 --- [      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-08-12 13:51:02.708 INFO 2124 --- [      main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2017-08-12 13:51:02.708 INFO 2124 --- [      main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2017-08-12 13:51:02.802 INFO 2124 --- [      main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2017-08-12 13:51:02.958 INFO 2124 --- [      main] o.s.j.e.a.AnnotationMBeanExporter    : Registering beans for JMX exposure on startup2017-08-12 13:51:03.083 INFO 2124 --- [      main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)2017-08-12 13:51:03.083 INFO 2124 --- [      main] com.test.HelloController         : Started HelloController in 3.329 seconds (JVM running for 3.61)2017-08-12 13:51:32.885 INFO 2124 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]    : Initializing Spring FrameworkServlet 'dispatcherServlet'2017-08-12 13:51:32.885 INFO 2124 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet    : FrameworkServlet 'dispatcherServlet': initialization started2017-08-12 13:51:32.900 INFO 2124 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet    : FrameworkServlet 'dispatcherServlet': initialization completed in 15 ms

  在url输入http://127.0.0.1:8080/hello

  页面将展示出来

 版权声明:本文为不会代码的小白原创文章,未经允许不得转载。