spring boot 带来的500怎么带message

用户名:randy_shandong
文章数:246
评论数:71
访问量:246704
注册日期:
阅读量:1297
阅读量:3317
阅读量:585131
阅读量:470724
51CTO推荐博文
本文主要演示如何使用Spring Boot加速应用开发的。你可以访问,快速构建属于你自己的基于Spring Boot的应用。如图,一键即可生成项目。1.开始构建项目1.1)项目结构1.2 pom.xml&?xml&version="1.0"&encoding="UTF-8"?&&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&org.springframework&/groupId&
&&&&&artifactId&gs-spring-boot&/artifactId&
&&&&&version&0.1.0&/version&
&&&&&parent&
&&&&&&&&&groupId&org.springframework.boot&/groupId&
&&&&&&&&&artifactId&spring-boot-starter-parent&/artifactId&
&&&&&&&&&version&1.3.5.RELEASE&/version&
&&&&&/parent&
&&&&&dependencies&
&&&&&&&&&dependency&
&&&&&&&&&&&&&groupId&org.springframework.boot&/groupId&
&&&&&&&&&&&&&artifactId&spring-boot-starter-web&/artifactId&
&&&&&&&&&/dependency&
&&&&&/dependencies&
&&&&&properties&
&&&&&&&&&java.version&1.8&/java.version&
&&&&&/properties&
&&&&&build&
&&&&&&&&&plugins&
&&&&&&&&&&&&&plugin&
&&&&&&&&&&&&&&&&&groupId&org.springframework.boot&/groupId&
&&&&&&&&&&&&&&&&&artifactId&spring-boot-maven-plugin&/artifactId&
&&&&&&&&&&&&&/plugin&
&&&&&&&&&/plugins&
&&&&&/build&&/project&提供以下支持:1.收集classpath下的jar,作为单个可运行的“über-jar”,这样可以更适合传输和执行。2.搜索public static void main(),标记为可运行的class3.提供内置依赖管理(Spring Boot dependencies.)1.3 使用spring boot能做什么?它提供快速构建应用的方式,搜索classpath和配置的bean,可以帮你从基础工作冲解脱出来,专心实现业务。1.使用springmvc?提供了几个bean,spring会自动加入;spring需要servlet容器支持,spring自动配置内嵌的tomcat。2.使用 jetty?不想使用tomcat,spring很容易进行切换3.Thymeleaf?也提供了支持。Thymeleaf 如果在classpath下,spring会自动加入SpringTemplateEngine 这仅仅是几个自动配置的场景。spring boot不会生成代码或修改你的文件,它会动态扩展bean和配置,并在应用中使用他们。1.4 创建一个简单的web应用package&
import&org.springframework.web.bind.annotation.RestC
import&org.springframework.web.bind.annotation.RequestM
@RestController
public&class&HelloController&{
&&&&@RequestMapping("/")
&&&&public&String&index()&{
&&&&&&&&return&"Greetings&from&Spring&Boot!";
}@RestController表示类准备使用SpringMVC处理web请求,绑定了@Controller和@ResponseBodypackage&org.springframework.web.bind.
@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Documented
@org.springframework.stereotype.Controller
@org.springframework.web.bind.annotation.ResponseBody
public&@interface&RestController&{
&&&&java.lang.String&value()&default&"";
}1.5创建一个应用Classpackage&
import&java.util.A
import&org.springframework.boot.SpringA
import&org.springframework.boot.autoconfigure.SpringBootA
import&org.springframework.context.ApplicationC
@SpringBootApplication
public&class&Application&{
&&&&public&static&void&main(String[]&args)&{
&&&&&&&&ApplicationContext&ctx&=&SpringApplication.run(Application.class,&args);
&&&&&&&&System.out.println("Let's&inspect&the&beans&provided&by&Spring&Boot:");
&&&&&&&&String[]&beanNames&=&ctx.getBeanDefinitionNames();
&&&&&&&&Arrays.sort(beanNames);
&&&&&&&&for&(String&beanName&:&beanNames)&{
&&&&&&&&&&&&System.out.println(beanName);
}@SpringBootApplication 也是1个组合体@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public&@interface&SpringBootApplication{
}@ComponentScan:告诉spring boot查找其他的components,configurations,services @EnableAutoConfiguration :告诉spring boot基于classpath添加bean,settings。一般情况下,你需要添加@EnableWebMvc,但spring boot在classpath中发现&spring-webmvc ,会自动添加@EnableWebMvc;SpringApplication.run() 启动应用入口,没有web.xml,没有application.xml.100%的java.输出结果如下D:\Java\jdk1.7.0_65\bin\java&-Didea.launcher.port=7535&"-Didea.launcher.bin.path=D:\Program&Files&(x86)\JetBrains\IntelliJ&IDEA&14.1.4\bin"&-Dfile.encoding=UTF-8&-classpath&"D:\Java\jdk1.7.0_65\jre\lib\charsets.D:\Java\jdk1.7.0_65\jre\lib\deploy.D:\Java\jdk1.7.0_65\jre\lib\javaws.D:\Java\jdk1.7.0_65\jre\lib\jce.D:\Java\jdk1.7.0_65\jre\lib\jfr.D:\Java\jdk1.7.0_65\jre\lib\jfxrt.D:\Java\jdk1.7.0_65\jre\lib\jsse.D:\Java\jdk1.7.0_65\jre\lib\management-agent.D:\Java\jdk1.7.0_65\jre\lib\plugin.D:\Java\jdk1.7.0_65\jre\lib\resources.D:\Java\jdk1.7.0_65\jre\lib\rt.D:\Java\jdk1.7.0_65\jre\lib\ext\access-bridge-64.D:\Java\jdk1.7.0_65\jre\lib\ext\dnsns.D:\Java\jdk1.7.0_65\jre\lib\ext\jaccess.D:\Java\jdk1.7.0_65\jre\lib\ext\localedata.D:\Java\jdk1.7.0_65\jre\lib\ext\sunec.D:\Java\jdk1.7.0_65\jre\lib\ext\sunjce_provider.D:\Java\jdk1.7.0_65\jre\lib\ext\sunmscapi.D:\Java\jdk1.7.0_65\jre\lib\ext\zipfs.E:\open\apache\spring-boot-example\target\D:\Documents\.m2\repository\org\thymeleaf\thymeleaf\2.1.4.RELEASE\thymeleaf-2.1.4.RELEASE.D:\Documents\.m2\repository\ognl\ognl\3.0.8\ognl-3.0.8.D:\Documents\.m2\repository\org\javassist\javassist\3.18.1-GA\javassist-3.18.1-GA.D:\Documents\.m2\repository\org\unbescape\unbescape\1.1.0.RELEASE\unbescape-1.1.0.RELEASE.D:\Documents\.m2\repository\org\slf4j\slf4j-api\1.7.21\slf4j-api-1.7.21.D:\Documents\.m2\repository\org\springframework\boot\spring-boot-starter-web\1.3.5.RELEASE\spring-boot-starter-web-1.3.5.RELEASE.D:\Documents\.m2\repository\org\springframework\boot\spring-boot-starter\1.3.5.RELEASE\spring-boot-starter-1.3.5.RELEASE.D:\Documents\.m2\repository\org\springframework\boot\spring-boot\1.3.5.RELEASE\spring-boot-1.3.5.RELEASE.D:\Documents\.m2\repository\org\springframework\spring-core\4.2.6.RELEASE\spring-core-4.2.6.RELEASE.D:\Documents\.m2\repository\org\springframework\spring-context\4.2.6.RELEASE\spring-context-4.2.6.RELEASE.D:\Documents\.m2\repository\org\springframework\spring-aop\4.2.6.RELEASE\spring-aop-4.2.6.RELEASE.D:\Documents\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.D:\Documents\.m2\repository\org\springframework\spring-beans\4.2.6.RELEASE\spring-beans-4.2.6.RELEASE.D:\Documents\.m2\repository\org\springframework\spring-expression\4.2.6.RELEASE\spring-expression-4.2.6.RELEASE.D:\Documents\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\1.3.5.RELEASE\spring-boot-autoconfigure-1.3.5.RELEASE.D:\Documents\.m2\repository\org\springframework\boot\spring-boot-starter-logging\1.3.5.RELEASE\spring-boot-starter-logging-1.3.5.RELEASE.D:\Documents\.m2\repository\ch\qos\logback\logback-classic\1.1.7\logback-classic-1.1.7.D:\Documents\.m2\repository\ch\qos\logback\logback-core\1.1.7\logback-core-1.1.7.D:\Documents\.m2\repository\org\slf4j\jcl-over-slf4j\1.7.21\jcl-over-slf4j-1.7.21.D:\Documents\.m2\repository\org\slf4j\jul-to-slf4j\1.7.21\jul-to-slf4j-1.7.21.D:\Documents\.m2\repository\org\slf4j\log4j-over-slf4j\1.7.21\log4j-over-slf4j-1.7.21.D:\Documents\.m2\repository\org\yaml\snakeyaml\1.16\snakeyaml-1.16.D:\Documents\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\1.3.5.RELEASE\spring-boot-starter-tomcat-1.3.5.RELEASE.D:\Documents\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\8.0.33\tomcat-embed-core-8.0.33.D:\Documents\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\8.0.33\tomcat-embed-el-8.0.33.D:\Documents\.m2\repository\org\apache\tomcat\embed\tomcat-embed-logging-juli\8.0.33\tomcat-embed-logging-juli-8.0.33.D:\Documents\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\8.0.33\tomcat-embed-websocket-8.0.33.D:\Documents\.m2\repository\org\springframework\boot\spring-boot-starter-validation\1.3.5.RELEASE\spring-boot-starter-validation-1.3.5.RELEASE.D:\Documents\.m2\repository\org\hibernate\hibernate-validator\5.2.4.Final\hibernate-validator-5.2.4.Final.D:\Documents\.m2\repository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.D:\Documents\.m2\repository\org\jboss\logging\jboss-logging\3.3.0.Final\jboss-logging-3.3.0.Final.D:\Documents\.m2\repository\com\fasterxml\classmate\1.1.0\classmate-1.1.0.D:\Documents\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.6.6\jackson-databind-2.6.6.D:\Documents\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.6.6\jackson-annotations-2.6.6.D:\Documents\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.6.6\jackson-core-2.6.6.D:\Documents\.m2\repository\org\springframework\spring-web\4.2.6.RELEASE\spring-web-4.2.6.RELEASE.D:\Documents\.m2\repository\org\springframework\spring-webmvc\4.2.6.RELEASE\spring-webmvc-4.2.6.RELEASE.D:\Program&Files&(x86)\JetBrains\IntelliJ&IDEA&14.1.4\lib\idea_rt.jar"&com.intellij.rt.execution.application.AppMain&hello.Application&--spring.output.ansi.enabled=always
&&.&&&____&&&&&&&&&&_&&&&&&&&&&&&__&_&_
&/\\&/&___'_&__&_&_(_)_&__&&__&_&\&\&\&\
(&(&)\___&|&'_&|&'_|&|&'_&\/&_`&|&\&\&\&\
&\\/&&___)|&|_)|&|&|&|&|&||&(_|&|&&)&)&)&)
&&'&&|____|&.__|_|&|_|_|&|_\__,&|&/&/&/&/
&=========|_|==============|___/=/_/_/_/
&::&Spring&Boot&::&&&&&&&&(v1.3.5.RELEASE)
&19:32:34.189&&INFO&10008&---&[&&&&&&&&&&&main]&hello.Application&&&&&&&&&&&&&&&&&&&&&&&&:&Starting&Application&on&zhaoguoyu-pc&with&PID&10008&(E:\open\apache\spring-boot-example\target\classes&started&by&Administrator&in&E:\open\apache\spring-boot-example)
&19:32:34.192&&INFO&10008&---&[&&&&&&&&&&&main]&hello.Application&&&&&&&&&&&&&&&&&&&&&&&&:&No&active&profile&set,&falling&back&to&default&profiles:&default
&19:32:34.256&&INFO&10008&---&[&&&&&&&&&&&main]&ationConfigEmbeddedWebApplicationContext&:&Refreshing&org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@12c11e94:&startup&date&[Sun&Jun&19&19:32:34&CST&2016];&root&of&context&hierarchy
&19:32:35.342&&INFO&10008&---&[&&&&&&&&&&&main]&s.b.c.e.t.TomcatEmbeddedServletContainer&:&Tomcat&initialized&with&port(s):&8080&(http)
&19:32:35.353&&INFO&10008&---&[&&&&&&&&&&&main]&o.apache.catalina.core.StandardService&&&:&Starting&service&Tomcat
&19:32:35.354&&INFO&10008&---&[&&&&&&&&&&&main]&org.apache.catalina.core.StandardEngine&&:&Starting&Servlet&Engine:&Apache&Tomcat/8.0.33
&19:32:35.423&&INFO&10008&---&[ost-startStop-1]&o.a.c.c.C.[Tomcat].[localhost].[/]&&&&&&&:&Initializing&Spring&embedded&WebApplicationContext
&19:32:35.423&&INFO&10008&---&[ost-startStop-1]&o.s.web.context.ContextLoader&&&&&&&&&&&&:&Root&WebApplicationContext:&initialization&completed&in&1169&ms
&19:32:35.642&&INFO&10008&---&[ost-startStop-1]&o.s.b.c.e.ServletRegistrationBean&&&&&&&&:&Mapping&servlet:&'dispatcherServlet'&to&[/]
&19:32:35.644&&INFO&10008&---&[ost-startStop-1]&o.s.b.c.embedded.FilterRegistrationBean&&:&Mapping&filter:&'characterEncodingFilter'&to:&[/*]
&19:32:35.645&&INFO&10008&---&[ost-startStop-1]&o.s.b.c.embedded.FilterRegistrationBean&&:&Mapping&filter:&'hiddenHttpMethodFilter'&to:&[/*]
&19:32:35.645&&INFO&10008&---&[ost-startStop-1]&o.s.b.c.embedded.FilterRegistrationBean&&:&Mapping&filter:&'httpPutFormContentFilter'&to:&[/*]
&19:32:35.645&&INFO&10008&---&[ost-startStop-1]&o.s.b.c.embedded.FilterRegistrationBean&&:&Mapping&filter:&'requestContextFilter'&to:&[/*]
&19:32:35.766&&INFO&10008&---&[&&&&&&&&&&&main]&s.w.s.m.m.a.RequestMappingHandlerAdapter&:&Looking&for&@ControllerAdvice:&org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@12c11e94:&startup&date&[Sun&Jun&19&19:32:34&CST&2016];&root&of&context&hierarchy
&19:32:35.812&&INFO&10008&---&[&&&&&&&&&&&main]&s.w.s.m.m.a.RequestMappingHandlerMapping&:&Mapped&"{[/]}"&onto&public&java.lang.String&hello.HelloController.index()
&19:32:35.815&&INFO&10008&---&[&&&&&&&&&&&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)
&19:32:35.815&&INFO&10008&---&[&&&&&&&&&&&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)
&19:32:35.836&&INFO&10008&---&[&&&&&&&&&&&main]&o.s.w.s.handler.SimpleUrlHandlerMapping&&:&Mapped&URL&path&[/webjars/**]&onto&handler&of&type&[class&org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
&19:32:35.836&&INFO&10008&---&[&&&&&&&&&&&main]&o.s.w.s.handler.SimpleUrlHandlerMapping&&:&Mapped&URL&path&[/**]&onto&handler&of&type&[class&org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
&19:32:35.875&&INFO&10008&---&[&&&&&&&&&&&main]&o.s.w.s.handler.SimpleUrlHandlerMapping&&:&Mapped&URL&path&[/**/favicon.ico]&onto&handler&of&type&[class&org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
&19:32:35.965&&INFO&10008&---&[&&&&&&&&&&&main]&o.s.j.e.a.AnnotationMBeanExporter&&&&&&&&:&Registering&beans&for&JMX&exposure&on&startup
&19:32:36.008&&INFO&10008&---&[&&&&&&&&&&&main]&s.b.c.e.t.TomcatEmbeddedServletContainer&:&Tomcat&started&on&port(s):&8080&(http)
&19:32:36.011&&INFO&10008&---&[&&&&&&&&&&&main]&hello.Application&&&&&&&&&&&&&&&&&&&&&&&&:&Started&Application&in&2.168&seconds&(JVM&running&for&2.426)
Let's&inspect&the&beans&provided&by&Spring&Boot:
application
basicErrorController
beanNameHandlerMapping
beanNameViewResolver
characterEncodingFilter
defaultServletHandlerMapping
defaultViewResolver
dispatcherServlet
dispatcherServletRegistration
duplicateServerPropertiesDetector
embeddedServletContainerCustomizerBeanPostProcessor
errorAttributes
errorPageCustomizer
faviconHandlerMapping
faviconRequestHandler
handlerExceptionResolver
helloController
hiddenHttpMethodFilter
httpPutFormContentFilter
httpRequestHandlerAdapter
jacksonObjectMapper
jacksonObjectMapperBuilder
mappingJackson2HttpMessageConverter
mbeanExporter
mbeanServer
messageConverters
multipart.CONFIGURATION_PROPERTIES
multipartConfigElement
multipartResolver
mvcContentNegotiationManager
mvcConversionService
mvcPathMatcher
mvcResourceUrlProvider
mvcUriComponentsContributor
mvcUrlPathHelper
mvcValidator
mvcViewResolver
objectNamingStrategy
org.springframework.boot.autoconfigure.AutoConfigurationPackages
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
org.springframework.boot.autoconfigure.condition.BeanTypeRegistry
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration
org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration
org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration$DispatcherServletConfiguration
org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration
org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat
org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration
org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration
org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration$StringHttpMessageConverterConfiguration
org.springframework.boot.autoconfigure.web.JacksonHttpMessageConvertersConfiguration
org.springframework.boot.autoconfigure.web.JacksonHttpMessageConvertersConfiguration$MappingJackson2HttpMessageConverterConfiguration
org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration
org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter$FaviconConfiguration
org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration
org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration$TomcatWebSocketConfiguration
org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor
org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.store
org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor
org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.event.internalEventListenerFactory
org.springframework.context.event.internalEventListenerProcessor
preserveErrorControllerTargetClassPostProcessor
propertySourcesPlaceholderConfigurer
requestContextFilter
requestMappingHandlerAdapter
requestMappingHandlerMapping
resourceHandlerMapping
serverProperties
simpleControllerHandlerAdapter
spring.http.encoding.CONFIGURATION_PROPERTIES
spring.jackson.CONFIGURATION_PROPERTIES
spring.mvc.CONFIGURATION_PROPERTIES
spring.resources.CONFIGURATION_PROPERTIES
stringHttpMessageConverter
tomcatEmbeddedServletContainerFactory
viewControllerHandlerMapping
viewResolver
websocketContainerCustomizer有好多的默认注入的bean. 这些bean就是spring boot强大功能的支持对象。你可以通过"tomcat"关键字查找,能找到tomcat的踪迹。可以跟踪源码。访问1.6添加测试用例在前面的pom.xml中添加&&&&&&&&&dependency&
&&&&&&&&&&&&&groupId&org.springframework.boot&/groupId&
&&&&&&&&&&&&&artifactId&spring-boot-starter-test&/artifactId&
&&&&&&&&&&&&&scope&test&/scope&
&&&&&&&&&/dependency&写一个简单的测试用例(这个是基于mock的方式)package&
import&static&org.hamcrest.Matchers.equalTo;
import&static&org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import&static&org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import&org.junit.B
import&org.junit.T
import&org.junit.runner.RunW
import&org.springframework.boot.test.SpringApplicationC
import&org.springframework.http.MediaT
import&org.springframework.mock.web.MockServletC
import&org.springframework.test.context.junit4.SpringJUnit4ClassR
import&org.springframework.test.context.web.WebAppC
import&org.springframework.test.web.servlet.MockM
import&org.springframework.test.web.servlet.request.MockMvcRequestB
import&org.springframework.test.web.servlet.setup.MockMvcB
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes&=&MockServletContext.class)
@WebAppConfiguration
public&class&HelloControllerTest&{
&&&private&MockMvc&
&&&@Before
&&&public&void&setUp()&throws&Exception&{
&&&&&&mvc&=&MockMvcBuilders.standaloneSetup(new&HelloController()).build();
&&&public&void&getHello()&throws&Exception&{
&&&&&&mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
&&&&&&&&&&&&.andExpect(status().isOk())
&&&&&&&&&&&&.andExpect(content().string(equalTo("Greetings&from&Spring&Boot!")));
}MockMvc来自spring-test模块,通过一组合适构造,发送HTTP请求。另外一个测试用例(full-stack integration test)package&
import&static&org.hamcrest.Matchers.equalTo;
import&static&org.junit.Assert.assertT
import&java.net.URL;
import&org.junit.B
import&org.junit.T
import&org.junit.runner.RunW
import&org.springframework.beans.factory.annotation.V
import&org.springframework.boot.test.IntegrationT
import&org.springframework.boot.test.SpringApplicationC
import&org.springframework.boot.test.TestRestT
import&org.springframework.http.ResponseE
import&org.springframework.test.context.junit4.SpringJUnit4ClassR
import&org.springframework.test.context.web.WebAppC
import&org.springframework.web.client.RestT
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes&=&Application.class)
@WebAppConfiguration
@IntegrationTest({"server.port=0"})
public&class&HelloControllerIT&{
&&&&@Value("${local.server.port}")
&&&&private&int&
&&&private&URL&
&&&private&RestTemplate&
&&&@Before
&&&public&void&setUp()&throws&Exception&{
&&&&&&this.base&=&new&URL("http://localhost:"&+&port&+&"/");
&&&&&&template&=&new&TestRestTemplate();
&&&public&void&getHello()&throws&Exception&{
&&&&&&ResponseEntity&String&&response&=&template.getForEntity(base.toString(),&String.class);
&&&&&&assertThat(response.getBody(),&equalTo("Greetings&from&Spring&Boot!"));
}@IntegrationTest("${server.port=0}")& 端口随机确定@Value("${local.server.port}")端口运行时确定。1.7添加生产环境支持需要在pom.xml添加&&&&&&&&&dependency&
&&&&&&&&&&&&&groupId&org.springframework.boot&/groupId&
&&&&&&&&&&&&&artifactId&spring-boot-starter-actuator&/artifactId&
&&&&&&&&&/dependency&重启应用,会看到增加了额外的日志&13:23:28.119&&...&:&Mapped&"{[/error],methods=[],params=[],headers=[],consumes...
&13:23:28.119&&...&:&Mapped&"{[/error],methods=[],params=[],headers=[],consumes...
&13:23:28.136&&...&:&Mapped&URL&path&[/**]&onto&handler&of&type&[class&org.spri...
&13:23:28.136&&...&:&Mapped&URL&path&[/webjars/**]&onto&handler&of&type&[class&...
&13:23:28.440&&...&:&Mapped&"{[/info],methods=[GET],params=[],headers=[],consum...
&13:23:28.441&&...&:&Mapped&"{[/autoconfig],methods=[GET],params=[],headers=[],...
&13:23:28.441&&...&:&Mapped&"{[/mappings],methods=[GET],params=[],headers=[],co...
&13:23:28.442&&...&:&Mapped&"{[/trace],methods=[GET],params=[],headers=[],consu...
&13:23:28.442&&...&:&Mapped&"{[/env/{name:.*}],methods=[GET],params=[],headers=...
&13:23:28.442&&...&:&Mapped&"{[/env],methods=[GET],params=[],headers=[],consume...
&13:23:28.443&&...&:&Mapped&"{[/configprops],methods=[GET],params=[],headers=[]...
&13:23:28.443&&...&:&Mapped&"{[/metrics/{name:.*}],methods=[GET],params=[],head...
&13:23:28.443&&...&:&Mapped&"{[/metrics],methods=[GET],params=[],headers=[],con...
&13:23:28.444&&...&:&Mapped&"{[/health],methods=[GET],params=[],headers=[],cons...
&13:23:28.444&&...&:&Mapped&"{[/dump],methods=[GET],params=[],headers=[],consum...
&13:23:28.445&&...&:&Mapped&"{[/beans],methods=[GET],params=[],headers=[],consu...尝试访问:http://localhost:8080/health...小计:本文章主要内容来自http://spring.io/guides/gs/spring-boot/。不是翻译文章,加自己的实践说明。真心佩服spring boot的优雅设置。扩展性,无侵入性都有很好的参照价值。以后有空在学习。下一步的工作:研读http://docs.spring.io/spring-boot/docs/1.3.5.RELEASE/reference/htmlsingle/本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)

我要回帖

更多关于 spring boot 自带容器 的文章

 

随机推荐