• springboot---->springboot的使用(一)


      这里我们记录一下springboot的使用,第一次创建环境。

    springboot的使用

    项目结构如下:

    一、我们使用maven去构建springboot的依赖。其中我们使用的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.linux.huhx</groupId>
        <artifactId>SpringBootTest</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>1.5.1.RELEASE</version>
            </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>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

    build里面是使用jre1.7去构建项目,我下载的maven默认是用1.5。依赖下载的jar包如下。

    spring-boot-starter-web-1.5.1.RELEASE.jar
    spring-boot-starter-1.5.1.RELEASE.jar
    spring-boot-1.5.1.RELEASE.jar
    spring-boot-autoconfigure-1.5.1.RELEASE.jar
    spring-boot-starter-logging-1.5.1.RELEASE.jar
    logback-classic-1.1.9.jar
    logback-core-1.1.9.jar
    slf4j-api-1.7.22.jar
    jcl-over-slf4j-1.7.22.jar
    jul-to-slf4j-1.7.22.jar
    log4j-over-slf4j-1.7.22.jar
    spring-core-4.3.6.RELEASE.jar
    snakeyaml-1.17.jar
    spring-boot-starter-tomcat-1.5.1.RELEASE.jar
    tomcat-embed-core-8.5.11.jar
    tomcat-embed-el-8.5.11.jar
    tomcat-embed-websocket-8.5.11.jar
    hibernate-validator-5.3.4.Final.jar
    validation-api-1.1.0.Final.jar
    jboss-logging-3.3.0.Final.jar
    classmate-1.3.1.jar
    jackson-databind-2.8.6.jar
    jackson-annotations-2.8.0.jar
    jackson-core-2.8.6.jar
    spring-web-4.3.6.RELEASE.jar
    spring-aop-4.3.6.RELEASE.jar
    spring-beans-4.3.6.RELEASE.jar
    spring-context-4.3.6.RELEASE.jar
    spring-webmvc-4.3.6.RELEASE.jar
    spring-expression-4.3.6.RELEASE.jar

    二、SimpleController.java的代码如下:

    package com.linux.huhx;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    @EnableAutoConfiguration
    public class SimpleController {
        @RequestMapping(value = "/hello", method = RequestMethod.GET)
        @ResponseBody
        public String hello() {
            return "hello world";
        }
    
        public static void main(String[] args) {
            SpringApplication.run(SimpleController.class, args);
        }
    }

    三、右击run,运行SimpleController的java项目。会有大致以下的输出日志。

      .   ____          _            __ _ _
     /\ / ___'_ __ _ _(_)_ __  __ _    
    ( ( )\___ | '_ | '_| | '_ / _` |    
     \/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v1.5.1.RELEASE)
    
    2017-02-24 17:43:31.997  INFO 19476 --- [           main] com.linux.huhx.SimpleController          : Starting SimpleController on Linux with PID 19476 (G:JavaJavaEEProgram2016-11-18SpringBootTest	argetclasses started by huhx in G:JavaJavaEEProgram2016-11-18SpringBootTest)
    2017-02-24 17:43:32.003  INFO 19476 --- [           main] com.linux.huhx.SimpleController          : No active profile set, falling back to default profiles: default
    2017-02-24 17:43:32.064  INFO 19476 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@293f78d3: startup date [Fri Feb 24 17:43:32 CST 2017]; root of context hierarchy
    2017-02-24 17:43:33.331  INFO 19476 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration' of type [class org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2017-02-24 17:43:33.408  INFO 19476 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'validator' of type [class org.springframework.validation.beanvalidation.LocalValidatorFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2017-02-24 17:43:33.942  INFO 19476 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
    2017-02-24 17:43:33.953  INFO 19476 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
    2017-02-24 17:43:33.954  INFO 19476 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.11
    2017-02-24 17:43:34.047  INFO 19476 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2017-02-24 17:43:34.048  INFO 19476 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1987 ms
    2017-02-24 17:43:34.219  INFO 19476 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
    2017-02-24 17:43:34.223  INFO 19476 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
    2017-02-24 17:43:34.224  INFO 19476 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
    2017-02-24 17:43:34.224  INFO 19476 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
    2017-02-24 17:43:34.225  INFO 19476 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
    2017-02-24 17:43:34.545  INFO 19476 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@293f78d3: startup date [Fri Feb 24 17:43:32 CST 2017]; root of context hierarchy
    2017-02-24 17:43:34.668  INFO 19476 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET]}" onto public java.lang.String com.linux.huhx.SimpleController.hello()
    2017-02-24 17:43:34.674  INFO 19476 --- [           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-02-24 17:43:34.674  INFO 19476 --- [           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-02-24 17:43:34.721  INFO 19476 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2017-02-24 17:43:34.721  INFO 19476 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2017-02-24 17:43:34.784  INFO 19476 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2017-02-24 17:43:35.042  INFO 19476 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
    2017-02-24 17:43:35.100  INFO 19476 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
    2017-02-24 17:43:35.105  INFO 19476 --- [           main] com.linux.huhx.SimpleController          : Started SimpleController in 3.557 seconds (JVM running for 4.123)

     四、在浏览器中输入: http://localhost:8080/hello。在页面会显示hello world字样。截图如下:

    注意url不是:http://localhost:8080/SpringBootTest/hello

    从中遇到的问题

    一、启动时报错:Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean

    解决方案:在SimpleController类上面,需要加上@EnableAutoConfiguration注解。

    二、输入url运行:Whitelabel Error Page  This application has no explicit mapping for /error, so you are seeing this as a fallback.

    解决方案:在SimpleController类上面,需要加上@Controller注解。

     友情链接

     springboot的使用: http://www.cnblogs.com/God-/p/5857428.html

  • 相关阅读:
    2016年总结,不一样的2016
    appium 遇到的坑
    Python xml 解析百度糯米信息
    Python 3.4 链接mysql5.7 数据库使用方法
    python3.x爬取美团信息
    基于python3的手机号生成脚本
    python3.x 学习心得
    H3C SNMP OID
    jython获取was5.1的jvm监控参数
    使用Jyhon脚本和PMI模块监控WAS性能数据
  • 原文地址:https://www.cnblogs.com/huhx/p/baseusespringboot1.html
Copyright © 2020-2023  润新知