我的第一篇博客spring boot学习
来源:互联网 发布:数据交换规范 编辑:程序博客网 时间:2024/05/27 06:15
spring boot最近貌似很火,号称(微服务).....不多说了上代码
看了很多篇博客,大都差不多,总结了下有几个注意的地方下面说明。
首先项目用maven构建。
先来看下目录结构
一、pom.xml(很关键)
<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>com</groupId> <artifactId>mySpringBoot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mySpringBoot</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.0.RELEASE</version> </parent> <dependencies> <!--springboot依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!--springboot依赖--> <!-- 模板引擎 类似jstl 更适合html5 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- spring热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <!--springboot修改代码立即生效--><!--调试的时候方便不加的话每次需要重启运行Application.java--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>utf8</encoding> </configuration> </plugin> </plugins> </build></project>
二、启动类Application.java
package mySpringBoot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;//@Configuration //@ComponentScan //@EnableAutoConfiguration//自动配置 @SpringBootApplication//和上面的三个注解作用相同public class Application {public static void main(String[] args) throws Exception {SpringApplication.run(Application.class, args);}}
直接运行main方法启动,这里卡了好久报个各种错误(具体情况以后再补充),启动成功如下
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.3.0.RELEASE)2016-09-20 19:17:33.800 INFO 31607 --- [ restartedMain] mySpringBoot.Application : Starting Application on jcz-Latitude-3550 with PID 31607 (/home/jcz/workspace/mySpringBoot/target/classes started by jcz in /home/jcz/workspace/mySpringBoot)2016-09-20 19:17:33.805 INFO 31607 --- [ restartedMain] mySpringBoot.Application : No profiles are active2016-09-20 19:17:34.295 INFO 31607 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@678b18dd: startup date [Tue Sep 20 19:17:34 CST 2016]; root of context hierarchy2016-09-20 19:17:36.076 INFO 31607 --- [ restartedMain] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$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$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]2016-09-20 19:17:37.071 INFO 31607 --- [ restartedMain] e.j.JettyEmbeddedServletContainerFactory : Server initialized with port: 80802016-09-20 19:17:37.078 INFO 31607 --- [ restartedMain] org.eclipse.jetty.server.Server : jetty-9.2.14.v201511062016-09-20 19:17:37.267 INFO 31607 --- [ restartedMain] application : Initializing Spring embedded WebApplicationContext2016-09-20 19:17:37.267 INFO 31607 --- [ restartedMain] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2974 ms2016-09-20 19:17:37.990 INFO 31607 --- [ restartedMain] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]2016-09-20 19:17:37.993 INFO 31607 --- [ restartedMain] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]2016-09-20 19:17:37.993 INFO 31607 --- [ restartedMain] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2016-09-20 19:17:37.994 INFO 31607 --- [ restartedMain] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]2016-09-20 19:17:37.994 INFO 31607 --- [ restartedMain] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]2016-09-20 19:17:38.293 INFO 31607 --- [ restartedMain] o.e.jetty.server.handler.ContextHandler : Started o.s.b.c.e.j.JettyEmbeddedWebAppContext@745e6f01{/,file:/tmp/jetty-docbase.2885017018384146467.8080/,AVAILABLE}2016-09-20 19:17:38.294 INFO 31607 --- [ restartedMain] org.eclipse.jetty.server.Server : Started @6514ms2016-09-20 19:17:38.482 INFO 31607 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@678b18dd: startup date [Tue Sep 20 19:17:34 CST 2016]; root of context hierarchy2016-09-20 19:17:38.591 INFO 31607 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/action2]}" onto public java.lang.String mySpringBoot.action.Action2.hello(org.springframework.ui.Model)2016-09-20 19:17:38.593 INFO 31607 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/action1]}" onto public java.lang.String mySpringBoot.action.Action1.action1()2016-09-20 19:17:38.600 INFO 31607 --- [ restartedMain] 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)2016-09-20 19:17:38.601 INFO 31607 --- [ restartedMain] 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-09-20 19:17:38.658 INFO 31607 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-09-20 19:17:38.658 INFO 31607 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-09-20 19:17:38.749 INFO 31607 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-09-20 19:17:39.580 INFO 31607 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 357292016-09-20 19:17:39.700 INFO 31607 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2016-09-20 19:17:39.732 INFO 31607 --- [ restartedMain] application : Initializing Spring FrameworkServlet 'dispatcherServlet'2016-09-20 19:17:39.732 INFO 31607 --- [ restartedMain] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started2016-09-20 19:17:39.764 INFO 31607 --- [ restartedMain] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 32 ms2016-09-20 19:17:39.869 INFO 31607 --- [ restartedMain] o.eclipse.jetty.server.ServerConnector : Started ServerConnector@52d2343d{HTTP/1.1}{0.0.0.0:8080}2016-09-20 19:17:39.872 INFO 31607 --- [ restartedMain] .s.b.c.e.j.JettyEmbeddedServletContainer : Jetty started on port(s) 8080 (http/1.1)2016-09-20 19:17:39.881 INFO 31607 --- [ restartedMain] mySpringBoot.Application : Started Application in 7.515 seconds (JVM running for 8.101)
三、写一个Action代码
package mySpringBoot.action;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController//是Controller的一种,HTTP Response的Body部分返回给浏览器@EnableAutoConfigurationpublic class Action1 {@RequestMapping("/action1")public String action1() {return "Hello SpringBoot action1";}}
好,接下来再写一个跳页面的Action
package mySpringBoot.action;import java.util.HashMap;import java.util.Map;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class Action2 { @RequestMapping("/action2")public String hello(Model model) { Map<String,Object> map=new HashMap<String,Object>();map.put("id", 1);map.put("name", "赵六");map.put("job","java coding man"); model.addAttribute("map", map); model.addAttribute("message", "你好"); return "hello";//跳转到hello页面}}
这里跳的不是jsp了,而是html5,请看pom引入了thymeleaf,好不多说,看页面代码
<!DOCTYPE HTML><html xmlns:th="http://www.thymeleaf.org"><head> <title>Getting Started: Serving Web Content</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body> <p th:text="'Hello, ' + ${map.name} + '!'" /> <p th:text="${map.id}+${map.name}+${map.job}" /></body></html>
是不是和jstl很像!!嘻嘻
到这里很容易就跑起来了spring项目了。
我启动时的错,引入tomcat目录bin下的tomcat-juli.jar启动成功。
有的错误还和pom.xml里的spring boot版本有关,具体的还待研究(也是群里各种问)。
注意:1、Application.java这个类所在位置要在最外层,这样才能spring扫描到它下面的所有action、serveice、dao等最后毕竟是最近比较火,有些不稳定莫名其妙报错写到这里,待续......
0 0
- 我的第一篇博客spring boot学习
- 我的第一篇博客
- 我的博客 第一篇
- 我的第一篇博客
- 我的第一篇博客
- 我的第一篇博客
- 我的第一篇博客
- 我的第一篇博客
- 我的第一篇博客
- 我的第一篇博客
- 我的第一篇博客
- 我的第一篇博客
- 我的第一篇博客
- 我的第一篇博客
- 我的第一篇博客
- 我的第一篇博客
- 我的第一篇博客
- 我的第一篇博客
- Leetcode 66 Plus One
- TCP协议小结一--TCP连接与释放以及TCP与UDP的区别
- HTML5学习_day01(4)--css内部样式之选择器
- Linux C/C++多线程学习:生产者消费者问题
- POJ_2431_Expedition
- 我的第一篇博客spring boot学习
- 组件(Component)
- 华为未来是铺满鲜花还是荆棘遍布?
- HDU 5900 QSC and Master (区间dp,记忆化搜索)
- jsp解析List
- Android Studio 2.2 来啦
- C++Primer 中文版第5版 习题3.31/3.32
- lesson07-17 各数据类型,printf,scanf函数格式
- Pig distinct by field