Spring Boot開發(一)-一個簡單的Spring Boot工程

0.開發前的準備

  • Spring Boot主頁:http://projects.spring.io/spring-boot/

  • Gradle使用

1.簡單的入門級的例子

  • Gradle配置

group 'org.ceecode'
version '1.0-SNAPSHOT'

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        //Spring Boot
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.5.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'spring-boot'

sourceCompatibility = 1.7

repositories {
    mavenCentral()
}

dependencies {
    testCompile("org.springframework.boot:spring-boot-starter-test")

    compile("org.springframework.boot:spring-boot-starter-web")
}
  • 核心代碼-Controller
@Controller
@EnableAutoConfiguration
@RequestMapping("/")
public class TestApiController {

    @RequestMapping("test")
    @ResponseBody
    public String test() {
        return "OK";
    }

}
  • 核心代碼-入口
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(TestApiController.class, args);
    }

}

2.運行例子

    例子的運行很簡單,直接執行Main方法便可.執行後的輸入日誌:html

2016-06-30 16:35:46.900  INFO 47306 --- [           main] Application                              : Starting Application on XUEQI-MACdeMacBook-Pro.local with PID 47306 (started by xueqi in /Users/xueqi/Development/Resource/IDEA Java/springboot-project)
2016-06-30 16:35:46.912  INFO 47306 --- [           main] Application                              : No active profile set, falling back to default profiles: default
2016-06-30 16:35:47.094  INFO 47306 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7f416310: startup date [Thu Jun 30 16:35:47 CST 2016]; root of context hierarchy
2016-06-30 16:35:49.692  INFO 47306 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-06-30 16:35:49.724  INFO 47306 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-06-30 16:35:49.727  INFO 47306 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.33
2016-06-30 16:35:49.881  INFO 47306 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-06-30 16:35:49.881  INFO 47306 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2795 ms
2016-06-30 16:35:50.249  INFO 47306 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-06-30 16:35:50.255  INFO 47306 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-06-30 16:35:50.255  INFO 47306 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-06-30 16:35:50.256  INFO 47306 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-06-30 16:35:50.256  INFO 47306 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2016-06-30 16:35:50.672  INFO 47306 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7f416310: startup date [Thu Jun 30 16:35:47 CST 2016]; root of context hierarchy
2016-06-30 16:35:50.763  INFO 47306 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test]}" onto public java.lang.String controller.TestApiController.test()
2016-06-30 16:35:50.770  INFO 47306 --- [           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)
2016-06-30 16:35:50.772  INFO 47306 --- [           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)
2016-06-30 16:35:50.818  INFO 47306 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-06-30 16:35:50.819  INFO 47306 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-06-30 16:35:50.869  INFO 47306 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-06-30 16:35:51.134  INFO 47306 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-06-30 16:35:51.242  INFO 47306 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-06-30 16:35:51.250  INFO 47306 --- [           main] Application                              : Started Application in 5.137 seconds (JVM running for 6.094)
2016-06-30 16:36:00.261  INFO 47306 --- [       Thread-2] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7f416310: startup date [Thu Jun 30 16:35:47 CST 2016]; root of context hierarchy
2016-06-30 16:36:00.265  INFO 47306 --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

訪問接口地址:java

相關文章
相關標籤/搜索