spring boot 入门hello web 以及错误处理

白又折腾了很久,毕竟要学习下框架。

似乎折腾出来觉得好简单啊!! 没有折腾出来时好忧伤啊~

环境: IDEA 2016  额JDK1.8 

  1. 创建一个maven project 
    关于maven 的学习,我就是先了解了下最基础可能也是对入门来说最核心的pom.xml 里面的项
  2. 开始传说中的2分钟搞定的web,反正我折腾了好久
    http://blog.csdn.net/xiaoyu411502/article/details/47864969

    -  在Maven项目依赖中引入spring-boot-starter-web: pom.xml //这个没什么问题了,反正groupId和artifactId是自己的就好

    -  创建 src/main/Java/Application.java 问题来了

    编译不过!!

    【第一个问题 error 46724】

    ERROR 46724 --- [ main] o.s.boot.SpringApplication : Application startup failed

    org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL

    Caused by: java.lang.ClassNotFoundException: org.springframework.dao.DataAccessException
    英文不够好的硬伤,去Google百度了很久。最后建立一个包搞定
    我竟然没注意

    ** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
    建立一个默认包好吗!

    【第一个问题 error 40304】

     ERROR 40304 --- [           main] o.apache.catalina.core.StandardService   : Failed to start connector [Connector[HTTP/1.1-8080]]

    好吧,这个很明显是端口不对。自然想到修改端口啊~

    尝试了各种方案 

    (1. 网上超多修改pom.xml 的配置  === 对入门的这个木有用

        2. 配置了个tomcat,下载安装,配置,运行,额 终于可以了 却404)

    最终方案

     public class Application extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer

    代码搞定一切

    注意这个运行时不是用的tomcat,而是直接用的application,(使用的是其内嵌默认的tomcat,而不是我配置的 )

    附最小白的源码 

/*
 * Copyright (C) 2016 Baidu, Inc. All Rights Reserved.
 */
package com.sfy.test;/**
 * Created by sunfangyuan on 2016/8/9.
 */

        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
        import org.springframework.boot.builder.SpringApplicationBuilder;
        import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
        import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
        import org.springframework.boot.context.web.SpringBootServletInitializer;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RestController;


@RestController
@SpringBootApplication

public class Application extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }

    @RequestMapping("/")
    public String index() {
        return "Index Page";
    }

    @RequestMapping("/hello")
    public String hello() {

        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        container.setPort(8081);
    }
}

         小白要继续学习啦~ 每次debug都会是一次成长吧,但我需要学会询问了! 加油!!

你可能感兴趣的:(java学习)