spring boot 学习小结1 spring mvc环境搭建

spring boot 官方指导文档地址
前段时间,了解到spring boot 比较适合用来快速进行 web项目开发,就有了研究下该框架怎么使用的想法。在搜索相关内容方面,发现比较少,于是就自己去看官方文档,终于成功搭建了一个demo。
运行结果如下图:
这里写图片描述

下面是环境搭建的具体步骤
1:创建一个webapp的maven项目,pom文件内容如下:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>improve.skn</groupId>
    <artifactId>demo</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.1.RELEASE</version>
    </parent>


    <dependencies>
        <!-- 该依赖定义了对spring mvc的支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 该依赖定义了对jsp的支持-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- 该依赖定义引用了tomcat相关jar包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> 
            <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> 
            <artifactId>spring-context</artifactId> <version>4.0.5.RELEASE</version> 
            </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> 
            <version>4.2.4.RELEASE</version> </dependency> -->

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.2.Final</version>
        </dependency>

        <!-- <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> 
            <version>4.3.2.Final</version> </dependency> -->
    </dependencies>
    <build>
        <finalName>demo</finalName>
    </build>
</project>

注意其中的parent元素,里面定义了很多的maven配置信息,我们直接继承就好了。

2:编写controller层、service层和dao层代码
controller代码:

@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping
    public String showLogin(){
        return "/WEB-INF/jsp/login.jsp";
    }

}

service代码(这里我只粘贴了impl的代码):

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao<User> userDao;
}

dao代码:

@Repository
public class UserDao<M> {

}

从上面可以看出来,我只是简单的分了个层,但是具体的业务代码我都没有去写,当然我这只是一个简单的demo项目,所以也不需要写。顺便说下,spring boot 1.3.1 引用的spring版本是4.2.4 Release。
接下来,最重要的步骤来了,就是配置spring boot启动类来启动demo。

3:配置spring boot 启动入口类,来启动整个spring boot demo项目
启动类代码如下:

@SpringBootApplication(scanBasePackages = {"improve.skn.demo.controller","improve.skn.demo.service.impl","improve.skn.demo.dao"})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@SpringBootApplication 的作用是标识这个类可以被spring boot框架识别,其中的scanBasePackages 属性的作用是定义注解扫描的目录。从代码中可以看出,分别取扫描了 controller、serviceimpl、dao 层的类。
直接运行Application类的main方法,启动spring boot 项目。

为了更容易理解,我把我的demo项目目录结构分享下:
这里写图片描述

再分享下目录spring boot启动成功的控制台信息


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.1.RELEASE)

2016-01-12 09:42:19.284  INFO 4196 --- [           main] i.skn.demo.application.Application       : Starting Application on shikenian with PID 4196 (C:\developer\workspace\demo\target\classes started by shikenian in C:\developer\workspace\demo)
2016-01-12 09:42:19.284  INFO 4196 --- [           main] i.skn.demo.application.Application       : No active profile set, falling back to default profiles: default
2016-01-12 09:42:19.401  INFO 4196 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6cbc2704: startup date [Tue Jan 12 09:42:19 CST 2016]; root of context hierarchy
2016-01-12 09:42:20.695  INFO 4196 --- [           main] 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-01-12 09:42:21.476  INFO 4196 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-01-12 09:42:21.492  INFO 4196 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-01-12 09:42:21.492  INFO 4196 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.30
2016-01-12 09:42:22.163  INFO 4196 --- [ost-startStop-1] org.apache.jasper.servlet.TldScanner     : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2016-01-12 09:42:22.163  INFO 4196 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-01-12 09:42:22.163  INFO 4196 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2762 ms
2016-01-12 09:42:22.632  INFO 4196 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-01-12 09:42:22.647  INFO 4196 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-01-12 09:42:22.647  INFO 4196 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-01-12 09:42:22.647  INFO 4196 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-01-12 09:42:22.647  INFO 4196 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2016-01-12 09:42:22.975  INFO 4196 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6cbc2704: startup date [Tue Jan 12 09:42:19 CST 2016]; root of context hierarchy
2016-01-12 09:42:23.037  INFO 4196 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/login]}" onto public java.lang.String improve.skn.demo.controller.LoginController.showLogin()
2016-01-12 09:42:23.037  INFO 4196 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET]}" onto public java.lang.String improve.skn.demo.controller.SpringBootTestController.hello()
2016-01-12 09:42:23.037  INFO 4196 --- [           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)
2016-01-12 09:42:23.053  INFO 4196 --- [           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)
2016-01-12 09:42:23.084  INFO 4196 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-12 09:42:23.084  INFO 4196 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-12 09:42:23.209  INFO 4196 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-12 09:42:23.475  INFO 4196 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-01-12 09:42:23.600  INFO 4196 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-01-12 09:42:23.600  INFO 4196 --- [           main] i.skn.demo.application.Application       : Started Application in 4.909 seconds (JVM running for 5.283)
2016-01-12 09:43:05.023  INFO 4196 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-01-12 09:43:05.023  INFO 4196 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2016-01-12 09:43:05.039  INFO 4196 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 16 ms
发布了66 篇原创文章 · 获赞 11 · 访问量 8万+
展开阅读全文

Springboot2.2.0.M1 +swagger2 报错

04-03

Description: An attempt was made to call a method that does not exist. The attempt was made from the following location: springfox.documentation.spring.web.plugins.DocumentationPluginsManager.createContextBuilder(DocumentationPluginsManager.java:152) The following method did not exist: org.springframework.plugin.core.PluginRegistry.getPluginFor(Ljava/lang/Object;Lorg/springframework/plugin/core/Plugin;)Lorg/springframework/plugin/core/Plugin; The method's class, org.springframework.plugin.core.PluginRegistry, is available from the following locations: jar:file:/E:/apache-maven-3.6.0/repository_all/org/springframework/plugin/spring-plugin-core/2.0.0.M1/spring-plugin-core-2.0.0.M1.jar!/org/springframework/plugin/core/PluginRegistry.class It was loaded from the following location: file:/E:/apache-maven-3.6.0/repository_all/org/springframework/plugin/spring-plugin-core/2.0.0.M1/spring-plugin-core-2.0.0.M1.jar Action: Correct the classpath of your application so that it contains a single, compatible version of org.springframework.plugin.core.PluginRegistry <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> 看情况是jar包冲突没有找到: ```org/springframework/plugin/core/Plugin 这个包是springfox-swagger2.jar依赖引入的,但是找不出是那里冲突了,为什么找不到类 ``` 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览