标签:except 运行 artifact sem xsd idt config nap ret
软件152 缑旭浩
1.新建maven项目,package方式为war.
使用maven的时候,maven会从仓库中下载依赖的jar包。
新建Maven Project 命名为:SpringBoot 选项如图
修改工程目录,添加源文件夹后目录如下:
2.在pom文件spring boot需要的依赖,配置文件如下:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 5 <groupId>com.lgp</groupId> 6 <artifactId>SpringBoot</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <packaging>jar</packaging> 9 10 <name>SpringBoot</name> 11 <url>http://maven.apache.org</url> 12 13 <properties> 14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 15 </properties> 16 17 <!-- Inherit defaults from Spring Boot --> 18 <parent> 19 <groupId>org.springframework.boot</groupId> 20 <artifactId>spring-boot-starter-parent</artifactId> 21 <version>1.4.0.BUILD-SNAPSHOT</version> 22 </parent> 23 24 <!-- Add typical dependencies for a web application --> 25 <dependencies> 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-web</artifactId> 29 </dependency> 30 <dependency> 31 <groupId>junit</groupId> 32 <artifactId>junit</artifactId> 33 <version>3.8.1</version> 34 <scope>test</scope> 35 </dependency> 36 </dependencies> 37 38 <!-- Package as an executable jar --> 39 <build> 40 <plugins> 41 <!-- <plugin> 42 <groupId>org.springframework.boot</groupId> 43 <artifactId>spring-boot-maven-plugin</artifactId> 44 </plugin> --> 45 46 <!-- Maven Assembly Plugin --> 47 <plugin> 48 <groupId>org.apache.maven.plugins</groupId> 49 <artifactId>maven-assembly-plugin</artifactId> 50 <version>2.4.1</version> 51 <configuration> 52 <!-- get all project dependencies --> 53 <descriptorRefs> 54 <descriptorRef>jar-with-dependencies</descriptorRef> 55 </descriptorRefs> 56 <!-- MainClass in mainfest make a executable jar --> 57 <archive> 58 <manifest> 59 <mainClass>com.lgp.SpringBoot.App</mainClass> 60 </manifest> 61 </archive> 62 63 </configuration> 64 <executions> 65 <execution> 66 <id>make-assembly</id> 67 <!-- bind to the packaging phase --> 68 <phase>package</phase> 69 <goals> 70 <goal>single</goal> 71 </goals> 72 </execution> 73 </executions> 74 </plugin> 75 76 </plugins> 77 </build> 78 79 80 81 <!-- Add Spring repositories --> 82 <!-- (you don‘t need this if you are using a .RELEASE version) --> 83 <repositories> 84 <repository> 85 <id>spring-snapshots</id> 86 <url>http://repo.spring.io/snapshot</url> 87 <snapshots><enabled>true</enabled></snapshots> 88 </repository> 89 <repository> 90 <id>spring-milestones</id> 91 <url>http://repo.spring.io/milestone</url> 92 </repository> 93 </repositories> 94 <pluginRepositories> 95 <pluginRepository> 96 <id>spring-snapshots</id> 97 <url>http://repo.spring.io/snapshot</url> 98 </pluginRepository> 99 <pluginRepository> 100 <id>spring-milestones</id> 101 <url>http://repo.spring.io/milestone</url> 102 </pluginRepository> 103 </pluginRepositories> 104 </project>
3.编辑JAVA代码新建APP.class
1 package com.lgp.SpringBoot; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RestController; 7 8 @RestController 9 @EnableAutoConfiguration 10 public class App 11 { 12 @RequestMapping("/h") 13 public String home() { 14 return "Hello"; 15 } 16 17 @RequestMapping("/w") 18 public String word() { 19 return "World"; 20 } 21 22 public static void main( String[] args ) 23 { 24 System.out.println( "Hello World ! App!" ); 25 SpringApplication.run(App.class, args); 26 //SpringApplication.run(UserController.class, args); 27 } 28 }
3.编写测试action,此action跟用其他方式创建spring项目里边的action没什么区别。
1 package com.example; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 import org.springframework.stereotype.Controller; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.ResponseBody; 8 9 @Controller 10 @EnableAutoConfiguration 11 public class HelloController { 12 13 @RequestMapping("/hello") 14 @ResponseBody 15 String home() { 16 return "Hello ,spring boot!"; 17 } 18 19 public static void main(String[] args) throws Exception { 20 SpringApplication.run(HelloController.class, args); 21 //运行之后在浏览器中访问:http://localhost:8080/hello 22 } 23 24 }
4.启动项目,只需要运行上面代码的main方法,运行成功,控制台输出如下
. ____ _ __ _ _ /\\ / ___‘_ __ _ _(_)_ __ __ _ \ \ \ ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ‘ |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.3.4.RELEASE) 2017-07-02 15:21:46.299 INFO 6668 --- [ main] com.example.HelloController : Starting HelloController on 0KE99GYM83Y5KGX with PID 6668 (D:\newspace\springbootdemo\target\classes started by Administrator in D:\newspace\springbootdemo) 2017-07-02 15:21:46.301 INFO 6668 --- [ main] com.example.HelloController : No active profile set, falling back to default profiles: default 2017-07-02 15:21:46.337 INFO 6668 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@18b8315: startup date [Tue May 10 15:21:46 CST 2016]; root of context hierarchy 2017-07-02 15:21:47.385 INFO 6668 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2017-07-02 15:21:47.399 INFO 6668 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat 2017-07-02 15:21:47.400 INFO 6668 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.33 2017-07-02 15:21:47.482 INFO 6668 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2017-07-02 15:21:47.482 INFO 6668 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1148 ms 2017-07-02 15:21:47.714 INFO 6668 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: ‘dispatcherServlet‘ to [/] 2017-07-02 15:21:47.718 INFO 6668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: ‘characterEncodingFilter‘ to: [/*] 2017-07-02 15:21:47.718 INFO 6668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: ‘hiddenHttpMethodFilter‘ to: [/*] 2017-07-02 15:21:47.718 INFO 6668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: ‘httpPutFormContentFilter‘ to: [/*] 2017-07-02 15:21:47.718 INFO 6668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: ‘requestContextFilter‘ to: [/*] 2017-07-02 15:21:47.939 INFO 6668 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@18b8315: startup date [Tue May 10 15:21:46 CST 2016]; root of context hierarchy 2017-07-02 15:21:47.991 INFO 6668 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto java.lang.String com.example.HelloController.home() 2017-07-02 15:21:47.993 INFO 6668 --- [ 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-07-02 15:21:47.994 INFO 6668 --- [ 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-07-02 15:21:48.014 INFO 6668 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-07-02 15:21:48.014 INFO 6668 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-07-02 15:21:48.048 INFO 6668 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-07-02 15:21:48.128 INFO 6668 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2017-07-02 15:21:48.187 INFO 6668 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2017-07-02 15:21:48.190 INFO 6668 --- [ main] com.example.HelloController : Started HelloController in 2.166 seconds (JVM running for 2.401) 2017-07-02 15:22:03.171 INFO 6668 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet ‘dispatcherServlet‘ 2017-07-02 15:22:03.171 INFO 6668 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet ‘dispatcherServlet‘: initialization started 2017-07-02 15:22:03.181 INFO 6668 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet ‘dispatcherServlet‘: initialization completed in 10 ms
5.在浏览器中访问,如下:
运行App.java 访问 http://localhost:8080/user/12
标签:except 运行 artifact sem xsd idt config nap ret
原文地址:http://www.cnblogs.com/javaeeadd/p/7106535.html