- 打开eclipse-->点击help-->点击eclipse marketplace-->点击search输入sts或者点击popular:
- 安装sts插件:
- 安装完成后点击file-->新建一个mave project,然后点击next:
- 到这个界面选择比较常用的maven-archetype-webapp,点击next:
- 到这个界面输入你的group ID和artifact Id:
- 然后点击finish:得到如下结构的maven项目:
- 编辑pom.xml引入springBoot:
-
<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.zd</groupId> <artifactId>helloSpringBoot</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>helloSpringBoot Maven Webapp</name> <url>http://maven.apache.org</url> <!-- spring-boot-starter-parent是Spring Boot的核心启动器, 包含了自动配置、日志和YAML等大量默认的配置,大大简化了我们的开发。 引入之后相关的starter引入就不需要添加version配置, spring boot会自动选择最合适的版本进行添加。 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version><!--$NO-MVN-MAN-VER$--> <scope>test</scope> </dependency> <!-- spring-boot-starter-web包含了Spring Boot预定义的一些Web开发的常用依赖包 如: spring-webmvc,Tomcat.... --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <finalName>helloSpringBoot</finalName> </build> </project>
在src/main/java新建测试类:
-
其中app类的代码为:
package com.zd.hellospringboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Hello world! * */ // 此注解指定这是一个SpringBoot的应用程序,不加就会报异常 Unable to start // ServletWebServerApplicationContext due to missing ServletWebServerFactory // bean @SpringBootApplication public class App { public static void main(String[] args) { // SpringApplication用于从main方法中启动Spring应用的类 SpringApplication.run(App.class, args); } }
这个类很重要,必须写名字随便取,这是springboot启动的入口,记住类上方要加上注解@SpringBootApplication,不然会报错
-
controller类的代码为:
-
package com.zd.hellospringboot; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.zd.hellospringboot.service.HelloSpringBootService; //RestController相当于SpringMVC中的 @Controller + @ResponseBody @RestController public class HelloController { @Autowired private HelloSpringBootService helloSpringBootService; // 映射"/hello"请求 @RequestMapping("/hello") public String hello() { return "Hello Spring Boot!"; } // 映射"/hello"请求 @RequestMapping("/hello2") public String hello2() { return "你好 spring boot!"; } }
确认上方的工作没问题后,我们启动项目,启动方法:右键项目名称--->run as-->spring boot app,然后console控制台出现以下信息:
-
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.0.RELEASE) 2018-09-04 13:51:58.120 INFO 17272 --- [ main] com.zd.hellospringboot.App : Starting App on zdAdmin with PID 17272 (D:\workspace\helloSpringBoot\target\classes started by zdAdmin in D:\workspace\helloSpringBoot) 2018-09-04 13:51:58.120 INFO 17272 --- [ main] com.zd.hellospringboot.App : No active profile set, falling back to default profiles: default 2018-09-04 13:51:58.168 INFO 17272 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5ea434c8: startup date [Tue Sep 04 13:51:58 CST 2018]; root of context hierarchy 2018-09-04 13:51:59.077 INFO 17272 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2018-09-04 13:51:59.099 INFO 17272 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-09-04 13:51:59.099 INFO 17272 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.28 2018-09-04 13:51:59.113 INFO 17272 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre1.8.0_172\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_172/bin/server;C:/Program Files/Java/jre1.8.0_172/bin;C:/Program Files/Java/jre1.8.0_172/lib/amd64;D:\app\zdAdmin\product\11.2.0\client_1\bin;F:\app\zdAdmin\product\11.2.0\client_1\bin;E:\app\zdAdmin\product\11.2.0\dbhome_1\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Java\jdk1.8.0_172\bin;E:\Program Files\VisualSVN Server\bin;E:\Program Files\TortoiseSVN\bin;E:\tools\apache-maven-3.5.3\bin;E:\ue;D:\app\zdAdmin\product\11.2.0\client_1\BIN;E:\app\zdAdmin\product\11.2.0\dbhome_1\BIN;C:\Users\zdAdmin\AppData\Local\Microsoft\WindowsApps;;E:\开发软件\eclipse;;.] 2018-09-04 13:51:59.192 INFO 17272 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-09-04 13:51:59.192 INFO 17272 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1041 ms 2018-09-04 13:51:59.293 INFO 17272 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 2018-09-04 13:51:59.309 INFO 17272 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2018-09-04 13:51:59.309 INFO 17272 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-09-04 13:51:59.309 INFO 17272 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2018-09-04 13:51:59.309 INFO 17272 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2018-09-04 13:51:59.512 INFO 17272 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5ea434c8: startup date [Tue Sep 04 13:51:58 CST 2018]; root of context hierarchy 2018-09-04 13:51:59.559 INFO 17272 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.zd.hellospringboot.HelloController.hello() 2018-09-04 13:51:59.559 INFO 17272 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello2]}" onto public java.lang.String com.zd.hellospringboot.HelloController.hello2() 2018-09-04 13:51:59.559 INFO 17272 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello3]}" onto public java.lang.String com.zd.hellospringboot.HelloController.hello3() 2018-09-04 13:51:59.559 INFO 17272 --- [ 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.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2018-09-04 13:51:59.559 INFO 17272 --- [ 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.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2018-09-04 13:51:59.606 INFO 17272 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-09-04 13:51:59.606 INFO 17272 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-09-04 13:51:59.621 INFO 17272 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-09-04 13:51:59.726 INFO 17272 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-09-04 13:51:59.773 INFO 17272 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2018-09-04 13:51:59.773 INFO 17272 --- [ main] com.zd.hellospringboot.App : Started App in 1.891 seconds (JVM running for 2.525) 2018-09-04 13:52:25.769 INFO 17272 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' 2018-09-04 13:52:25.769 INFO 17272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 2018-09-04 13:52:25.787 INFO 17272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 18 ms
没有报错的话我们访问路径执行方法:
-
成功执行,记得点赞哦各位大佬
eclipse 开发spring boot 入门级程序
最新推荐文章于 2023-06-07 23:29:29 发布