springboot和web整合

摘要 spring-boot整合web为啥能有这么多问题,好几天都是崩溃的。本文我就一个问题一个问题的交代,并且给出我的解决方案。解决方案未必是通用的,我还是建议大家边跟代码边猜测,找到解决途径。最坑的就是百度那么多烂文章,耗费时间不解决问题。

问题1: Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling 不能正常跳转到jsp文件.

问题定位:

我走了下Dispatcher、AbstractView、JstlView这三个类。发现有一个参数一直是异常的:_contextHandler

如果正常它应该是我们项目信息:

o.m.j.p.JettyWebAppContext{/es-web,file:/D:/材料/源码/zhangkaitao/123/es-master/web/src/main/webapp/},file:/D://es-master/web/src/main/webapp/

而在我的项目里,它是这个数据:

o.s.b.c.e.j.JettyEmbeddedWebAppContext@527d5e48{/,file:///C:/Users/user/AppData/Local/Temp/jetty-docbase.4093955487572732783.8080/,AVAILABLE} 很显然,这个数据应该是配合错误的,这个类JettyEmbeddedWebAppContext是springboot里面的。打开它的父类,发现自己真的是太浅薄了,原来这个类也是可以配置很多信息的。这个参数太底层了,我们无法直接操作,那么jetty给了我们的配置工厂是AbstractEmbeddedServletContainerFactory,通过它能够解决。 想了半天如何添加,也疑惑为什么项目根目录是这个c:文件夹,后来我将MyApplication里面的pplication.sources(MyApplication.class) 改成pplication.sources(this.getClass());就好使了。这一点一定跟加载机制有关系,深入我不清楚。

问题解决:

@SpringBootApplication(exclude = PersistenceExceptionTranslationAutoConfiguration.class)
@EnableConfigurationProperties
@ComponentScan("com.framework.demo")
public class MyApplication extends SpringBootServletInitializer {

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(this.getClass());
    }
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

问题2:不要直接的运行MyApplication文件。 虽然网上一大堆直接运行MyApplication能够顺利的访问到项目资源,可是我直接运行确实找不到jsp页面的。所以这里建议用spring-boot的插件运行(mvn的spring-boot:run启动)。

 <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.framework.demo.web.MyApplication</mainClass>
                    <addResources>true</addResources>
                    <layout>ZIP</layout>
                    <classifier>exec</classifier>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>

            </plugin>

问题3:mvn install的时候测试类、文档类会出现问题

这个问题虽然最好找到解决方案,但是如果你没有时间,很着急,那么使用以下命令暂时绕过也是不过的选择。

install -D maven.javadoc.skip=true -D maven.test.skip=true

问题4:打包出错 repackage failed: Source must refer to an existing file sprinboot的打包是二次打包,必须是在已有包的基础上才能够打包成功(war、jar)

spring-boot:run命令需要有正确的依赖关系才能启动,如果使用spring-boot打包肯定会找不到model之间的依赖关系。所以配置要改下。

 <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.framework.demo.web.MyApplication</mainClass>
                    <addResources>true</addResources>
                    <layout>ZIP</layout>
                    //把原来的包install到本地私服中,如果不配置放入私服中的是springboot形式的包,结构完全不同,不同model之间的依赖关系将被破坏。
                    <classifier>exec</classifier>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>

                        </goals>
                    </execution>
                </executions>

            </plugin>

问题5、spring-boot打包

spring-boot:repackage是二次打包命令,需要我们事先打包好war。

spring-boot 之所以能够在程序打完包之后直接就能够运行,是因为它是将我们项目包包裹在了自己的运行包中,进行了二次打包,深入的有待接下来的研究。下面给出maven 的war包打包插件,注意下,如果项目中没有了web.xml,一定要将failOnMissingWebXml加上。

 <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <webappDirectory>${project.build.directory}/${module.war.name}.war</webappDirectory>
                    <warSourceDirectory>src/main/webapp</warSourceDirectory>
                    <webResources>
                        <resource>
                            <directory>src/main/resources</directory>
                            <includes>
                                <include>**/*.*</include>
                            </includes>
                            <filtering>true</filtering>
                            <targetPath>WEB-INF/classes/META-INF</targetPath>
                        </resource>
                        <resource>
                            <directory>src/main/webapp/WEB-INF</directory>
                            <filtering>true</filtering>
                            <targetPath>WEB-INF</targetPath>
                            <includes>
                                <include>log4j.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                    <resourceEncoding>UTF-8</resourceEncoding>
                </configuration>
                <executions>
                    <execution>
                        <id>default-war</id>
                        <phase>package</phase>
                        <goals>
                            <goal>war</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.framework.demo.web.MyApplication</mainClass>
                    <addResources>true</addResources>
                    <layout>ZIP</layout>
                    <classifier>exec</classifier>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>

            </plugin>

问题:静态文件访问不到

网上好多文章有点儿忽悠人的成分,能不能说清楚类路径先,然后在说spring-boot下面的默认资源路径在哪里放置,害得我看到页面正常访问的那一刻内心是崩溃的。spring-boot有五个默认的静态资源放置路径:

1、locations=[ServletContext resource [/],

2、class path resource [META-INF/resources/],

3、class path resource [resources/],

4、class path resource [static/],

5、class path resource [public/]

请各位注意,这块和我们平时配置是不同的。这里面说的是类路径,也就是说我们平时java文件编译成class文件那个目录下。而webapp下面springboot是不会去找的。另外,一些文字说这样添加路径,classpath:WEB-INF/,这种提示很容易让人误解是和我们之前的配置习惯一致,害人不浅!!配置的时候一定要记得是类路径,不是我们之前的webapp路径下!

欢迎来我的公众号:hyssop的后花园

参考文献 https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples http://docs.spring.io/spring-boot/docs/current/maven-plugin/index.html

转载于:https://my.oschina.net/zjItLife/blog/776168

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值