Spring Boot实践应用开发(3)

35 篇文章 0 订阅
9 篇文章 0 订阅

【创建第一个项目】

1,启动D:\sts-bundle\sts-3.6.4.RELEASE\STS.exe,输入workspace为D:\sts\workspace。勾选Uset his as default and do not ask again。

 

2,在D:\sts\workspace下创建项目文件夹first。

3,在first下建以下文件夹,

        D:\ sts\workspace\first\src\main\java\org\com\first\

        D:\ sts\workspace\first\src\test\java\org\com\first\

4,新规java程序FirstExample.java如下,

packageorg.com.first;
 
importorg.springframework.boot.*;
importorg.springframework.boot.autoconfigure.*;
importorg.springframework.web.bind.annotation.*;
 
@RestController
@EnableAutoConfiguration
publicclass FirstExample {
       @RequestMapping("/")
       String firsthome() {
              return"Welcome to firstdemo!";
       }
 
       publicstaticvoid main(String[] args) throws Exception {
              SpringApplication.run(FirstExample.class, args);
       }
}

5,项目根目录下创建build.gradle,

buildscript {
    repositories {
        jcenter()
        maven { url"http://repo.spring.io/snapshot" }
        maven { url"http://repo.spring.io/milestone" }
    }
    dependencies {
       classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.3.RELEASE")
    }
}
 
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
 
jar {
    baseName = 'first'
    version =  '0.1.0'
}
 
repositories {
    jcenter()
    maven { url"http://repo.spring.io/snapshot" }
    maven { url"http://repo.spring.io/milestone" }
}
 
sourceCompatibility = 1.8
targetCompatibility = 1.8
 
dependencies {   
   compile("org.springframework.boot:spring-boot-starter-web")
   testCompile("org.springframework.boot:spring-boot-starter-test")
}
 
task wrapper(type: Wrapper) {
    gradleVersion ='2.3'
}

6,执行gradle wrapper,生成gradle wrapper gradlew.bat

D:\sts\workspace\first>gradlewrapper
:wrapper
 
BUILDSUCCESSFUL

7,执行gradlew build,

D:\sts\workspace\first>gradlewbuild
:compileJava
:processResourcesUP-TO-DATE
:classes
:jar
:findMainClass
:startScripts
:distTar
:distZip
:bootRepackage
:assemble
:compileTestJavaUP-TO-DATE
:processTestResourcesUP-TO-DATE
:testClassesUP-TO-DATE
:testUP-TO-DATE
:checkUP-TO-DATE
:build
 
BUILDSUCCESSFUL


8,执行gradlew eclipse,转换为eclipse项目,并用spring tool suite导入此项目。

D:\sts\workspace\first>gradleweclipse
:eclipseClasspath
:eclipseJdt
:eclipseProject
:eclipse
 
BUILDSUCCESSFUL


 

9,执行gradlewbootRun启动项目,

D:\sts\workspace\first>gradlew bootRun
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:findMainClass
:bootRun
 
  .   ____         _            __ _ _
 /\\ / ___'_ __ __(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot::        (v1.2.3.RELEASE)
 
2015-05-06 19:43:28.379 INFO 12888 --- [           main]org.com.first.FirstExample              : Starting FirstExample on Lily with PID 12888 (D:\
sts\workspace\first\build\classes\main started by Shin inD:\sts\workspace\first)
2015-05-06 19:43:28.449 INFO 12888 --- [           main]ationConfigEmbeddedWebApplicationContext : Refreshingorg.springframework.boot.context.embed
ded.AnnotationConfigEmbeddedWebApplicationContext@1b5cd00:startup date [Wed May 06 19:43:28 JST 2015]; root of context hierarchy
2015-05-06 19:43:28.984 INFO 12888 --- [           main]o.s.b.f.s.DefaultListableBeanFactory    : Overriding bean definition for bean 'beanNameView
Resolver': replacing [Root bean: class [null]; scope=;abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0;autowireCandidate=true; prim
ary=false;factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration;factoryMethodName=be
anNameViewResolver; initMethodName=null; destroyMethodName=(inferred);defined in class path resource [org/springframework/boot/autoconfigure/web/Erro
rMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]]with [Root bean: class [null]; scope=; abstract=false; lazyInit=false;autowireMode=3;
dependencyCheck=0; autowireCandidate=true; primary=false;factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAut
oConfigurationAdapter;factoryMethodName=beanNameViewResolver; initMethodName=null;destroyMethodName=(inferred); defined in class path resource [org/
springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2015-05-06 19:43:37.265 INFO 12888 --- [           main]s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s):8080 (http)
2015-05-06 19:43:37.596 INFO 12888 --- [           main]o.apache.catalina.core.StandardService  : Starting service Tomcat
2015-05-06 19:43:37.598 INFO 12888 --- [           main]org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.20
2015-05-06 19:43:37.740 INFO 12888 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embeddedWebApplicationContex
t
2015-05-06 19:43:37.740 INFO 12888 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext:initialization comple
ted in 9293 ms
2015-05-06 19:43:38.546 INFO 12888 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet'to [/]
2015-05-06 19:43:38.553 INFO 12888 --- [ost-startStop-1]o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*
]
2015-05-06 19:43:38.554 INFO 12888 --- [ost-startStop-1]o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
 
2015-05-06 19:43:38.889 INFO 12888 --- [           main]s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice:org.springframewor
k.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1b5cd00:startup date [Wed May 06 19:43:28 JST 2015]; root of context hierarchy
2015-05-06 19:43:38.961 INFO 12888 --- [           main]s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped"{[/],methods=[],params=[],headers=[],cons
umes=[],produces=[],custom=[]}" onto java.lang.Stringorg.com.first.FirstExample.firsthome()
2015-05-06 19:43:38.964 INFO 12888 --- [           main]s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped"{[/error],methods=[],params=[],headers=[]
,consumes=[],produces=[],custom=[]}" onto publicorg.springframework.http.ResponseEntity<java.util.Map<java.lang.String,java.lang.Object>> org.spring
framework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2015-05-06 19:43:38.964 INFO 12888 --- [           main]s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped"{[/error],methods=[],params=[],headers=[]
,consumes=[],produces=[text/html],custom=[]}" ontopublic org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.Basi
cErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
2015-05-06 19:43:39.001 INFO 12888 --- [           main]o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of typ
e [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-05-06 19:43:39.001 INFO 12888 --- [           main]o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class
 org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-05-06 19:43:39.064 INFO 12888 --- [           main]o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of
 type [classorg.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-05-06 19:43:39.151 INFO 12888 --- [           main]o.s.j.e.a.AnnotationMBeanExporter       : Registering beans for JMX exposure on startup
2015-05-06 19:43:39.262 INFO 12888 --- [           main]s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080(http)
2015-05-06 19:43:39.264 INFO 12888 --- [           main]org.com.first.FirstExample              : Started FirstExample in 11.192 seconds (JVM runni
ng for 11.65)
2015-05-06 19:43:39.444 INFO 12888 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet'dispatcherS
ervlet'
2015-05-06 19:43:39.445 INFO 12888 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet':initializat
ion started
2015-05-06 19:43:39.474 INFO 12888 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet':initializat
ion completed in 29 ms
> Building 80% > :bootRun


10,打开浏览器,输入http://localhost:8080/


或者输入curl http://localhost:8080/访问,



下载代码first.zip,http://pan.baidu.com/s/1o6spOJS


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值