java - 使用 Spring Boot 在多模块项目中加载静态内容时出错

标签 java spring-boot maven weblogic multi-module

我有一个带有 Spring boot 和外部服务器 Weblogic 的多模块项目。 这些是模块:

  • 服务
  • 网络

    pom.xml (dao) 。

这是与数据库(存储库,实体)一起工作

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>gov-multiple-modules</artifactId>
        <groupId>gov</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>


    <groupId>com.dao</groupId>
    <artifactId>dao</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <output.directory.jdbc.oracle>${project.basedir}/src/main/resources</output.directory.jdbc.oracle>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc</artifactId>
            <version>6</version>
            <scope>system</scope>
            <systemPath>${output.directory.jdbc.oracle}/lib/ojdbc6.jar</systemPath>
        </dependency>

    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <argLine>-Dfile.encoding=UTF8</argLine>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

  • pom.xml(服务)
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>gov-multiple-modules</artifactId>
        <groupId>gov</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.service</groupId>
    <artifactId>service</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.dao</groupId>
            <artifactId>dao</artifactId>
            <version>${version.dao.module}</version>
        </dependency>


        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${version.mapstruct}</version>
        </dependency>

    </dependencies>


    <build>
          <plugins>

              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-surefire-plugin</artifactId>
                  <configuration>
                      <argLine>-Dfile.encoding=UTF8</argLine>
                  </configuration>
              </plugin>

            <plugin> 
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${version.apache.maven.plugins}</version>
                <groupId>org.apache.maven.plugins</groupId>

                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${version.mapstruct}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

  • pom.xml(网络)

    它是处理来自客户端(Contoroller 和 RestControllers)的请求。

应用程序中有一个入口点。

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>gov-multiple-modules</artifactId>
        <groupId>gov</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.web</groupId>
    <artifactId>web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.service</groupId>
            <artifactId>service</artifactId>
            <version>${version.service.module}</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>weblogic-war-gov</finalName>
        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <argLine>-Dfile.encoding=UTF8</argLine>
                </configuration>
            </plugin>


            <plugin> <!--It is for convert beans-->
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${version.apache.maven.plugins}</version>
                <groupId>org.apache.maven.plugins</groupId>

                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${version.mapstruct}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

  • pom.xml(父级)
<?xml version="1.0" encoding="UTF-8"?>
<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
         https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <modules>
        <module>dao</module>
        <module>service</module>
        <module>web</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>gov</groupId>
    <artifactId>gov-multiple-modules</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>gov-multiple-modules</name>
    <description>project with Spring Boot for multiple module applications</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <version.apache.maven.plugins>3.8.1</version.apache.maven.plugins>
        <version.mapstruct>1.3.0.Final</version.mapstruct>

        <version.apache.common.lang3>3.9</version.apache.common.lang3>
        <version.apache.commons.text>1.8</version.apache.commons.text>
        <version.apache.commons.beanutils>1.9.4</version.apache.commons.beanutils>
        <version.hibernate.validator>6.0.17.Final</version.hibernate.validator>
        <version.reflection>0.9.11</version.reflection>
        <version.dao.module>0.0.1-SNAPSHOT</version.dao.module>
        <version.service.module>0.0.1-SNAPSHOT</version.service.module>

    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--This artifact need for testing that to find classes into classpath-->
        <dependency>
            <groupId>org.reflections</groupId>
            <artifactId>reflections</artifactId>
            <version>${version.reflection}</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${version.apache.common.lang3}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-text</artifactId>
            <version>${version.apache.commons.text}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>${version.apache.commons.beanutils}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

    </dependencies>

</project>

入口点

@SpringBootConfiguration
@SpringBootApplication
@EnableJpaRepositories(basePackages = {"com.dao", "com.service"})
@EntityScan(basePackages = {"com.dao"})
@ComponentScan(basePackages = {"com.service", "com.dao", "com.web"})
public class WebSpringBootJarApplication
        extends SpringBootServletInitializer
        implements WebApplicationInitializer {

    private static final Logger LOGGER  = LoggerFactory.getLogger( WebSpringBootJarApplication.class );

    public static void main(String[] args) {
        SpringApplication.run(WebSpringBootJarApplication.class, args);
        LOGGER.info("Start an application...");
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        LOGGER.info("There is building the web application!");
        return builder.sources(WebSpringBootJarApplication.class);
    }
}
  • src/main/webapp/WEB-INF/weblogic.xml
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app
        xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        https://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd
        http://xmlns.oracle.com/weblogic/weblogic-web-app
        https://xmlns.oracle.com/weblogic/weblogic-web-app/1.9/weblogic-web-app.xsd">

    <wls:context-root>sun</wls:context-root>
</wls:weblogic-web-app>

运行后我必须看到页面的问候语。

  • com.web.controller.index.IndexController
@Controller
public class IndexController {

    @RequestMapping(value="/", method= RequestMethod.GET)
    public String index() {
        return "index";
    }
}

  • src/main/resources/templates/index.html

但是我收到错误。

http://localhost:7001/sun/

  • 在浏览器中

Error 500--Internal Server Error java.lang.NullPointerException at weblogic.servlet.internal.ServletResponseImpl.sendContentError(ServletResponseImpl.java:738) at weblogic.servlet.internal.ServletResponseImpl.sendError(ServletResponseImpl.java:796) at weblogic.servlet.internal.ServletResponseImpl.sendError(ServletResponseImpl.java:713) at org.springframework.boot.web.servlet.support.ErrorPageFilter$ErrorWrapperResponse.sendErrorIfNecessary(ErrorPageFilter.java:349) at org.springframework.boot.web.servlet.support.ErrorPageFilter$ErrorWrapperResponse.getWriter(ErrorPageFilter.java:363) at org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$StaticView.render(ErrorMvcAutoConfiguration.java:227) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1118)

  • 在控制台 IDE 中

-"[2020-03-11 08:53:30,118] Artifact web:war exploded: Artifact is deployed successfully [2020-03-11 08:53:30,119] Artifact web:war exploded: Deploy took 14,724 milliseconds --11-03-2020 08:53:30.612 - INFO 17568 o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' -"--11-03-2020 08:53:30.613 -DEBUG 17568 o.s.web.servlet.DispatcherServlet : Detected StandardServletMultipartResolver -"--11-03-2020 08:53:30.621 -DEBUG 17568 o.s.web.servlet.DispatcherServlet : enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data -"--11-03-2020 08:53:30.621 - INFO 17568 o.s.web.servlet.DispatcherServlet : Completed initialization in 8 ms -"--11-03-2020 08:53:30.624 -DEBUG 17568 o.s.web.servlet.DispatcherServlet : GET "/sun/", parameters={} -"--11-03-2020 08:53:30.628 -DEBUG 17568 s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.web.controller.index.IndexController#index() -"--11-03-2020 08:53:30.647 -DEBUG 17568 o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, image/gif, image/jpeg, /;q=.2] -"--11-03-2020 08:53:30.647 -DEBUG 17568 o.s.web.servlet.view.JstlView : View name 'index', model {} -"--11-03-2020 08:53:30.652 -DEBUG 17568 o.s.web.servlet.view.JstlView : Forwarding to [index] -"--11-03-2020 08:53:30.653 -DEBUG 17568 o.s.web.servlet.DispatcherServlet : "FORWARD" dispatch for GET "/sun/index", parameters={} -"--11-03-2020 08:53:30.656 -DEBUG 17568 o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"] -"--11-03-2020 08:53:30.657 -DEBUG 17568 o.s.w.s.r.ResourceHttpRequestHandler : Resource not found -"--11-03-2020 08:53:30.657 -DEBUG 17568 o.s.web.servlet.DispatcherServlet : Exiting from "FORWARD" dispatch, status 404 -"--11-03-2020 08:53:30.664 -DEBUG 17568 o.s.web.servlet.DispatcherServlet : Error rendering view [org.springframework.web.servlet.view.JstlView: name 'index'; URL [index]] - java.lang.NullPointerException: null at weblogic.servlet.internal.ServletResponseImpl.sendContentError(ServletResponseImpl.java:738) ~[com.oracle.weblogic.servlet.jar:12.2.1.4] at weblogic.servlet.internal.ServletResponseImpl.sendError(ServletResponseImpl.java:796) ~[com.oracle.weblogic.servlet.jar:12.2.1.4] at weblogic.servlet.internal.ServletResponseImpl.sendError(ServletResponseImpl.java:713) ~[com.oracle.weblogic.servlet.jar:12.2.1.4] at org.springframework.boot.web.servlet.support.ErrorPageFilter$ErrorWrapperResponse.sendErrorIfNecessary(ErrorPageFilter.java:349) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE] at org.springframework.boot.web.servlet.support.ErrorPageFilter$ErrorWrapperResponse.flushBuffer(ErrorPageFilter.java:343) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE] at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:336) ~[com.oracle.weblogic.servlet.jar:12.2.1.4] at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:171) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE] at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:316) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE] at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373) [spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE] at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1118) [spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057) [spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) [spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) [spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) [spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:687) [javax.servlet.javax.servlet-api.jar:3.1.0] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) [spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE] ...

“java.lang.NullPointerException:空在 weblogic.servlet.internal.ServletResponseImpl.sendContentError(ServletResponseImpl.java:738) 在 weblogic.servlet.internal.ServletResponseImpl.sendError(ServletResponseImpl.java:796) 在 weblogic.servlet.internal.ServletResponseImpl.sendError(ServletResponseImpl.java:713) 在 org.springframework.boot.web.servlet.support.ErrorPageFilter$ErrorWrapperResponse.sendErrorIfNecessary(ErrorPageFilter.java:349) 在 org.springframework.boot.web.servlet.support.ErrorPageFilter$ErrorWrapperResponse.flushBuffer(ErrorPageFilter.java:343) 在 weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:336) 在 org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:171) ...

--11-03-2020 08:53:30.666 -DEBUG 17568 o.s.web.servlet.DispatcherServlet:无法完成 请求:java.lang.NullPointerException -”--11-03-2020 08:53:30.667 -错误 17568 o.s.b.w.servlet.support.ErrorPageFilter : 由于异常 [null],从请求 [/] 转发到错误页面

它工作时应用程序不是多模块项目。

知道错误是什么吗?请。

最佳答案

解决方案

我需要添加一个依赖项 - thymeleaf。没有静态内容就无法运行。

需要添加到pom的web-module中。

 <groupId>com.web</groupId>
    <artifactId>web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

关于java - 使用 Spring Boot 在多模块项目中加载静态内容时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60630446/

相关文章:

java - 多对多的 JPA Criteria API 规范

java - eUML 和 Eclipse 开普勒 : Has someone been able to make it work?

java - 是否可以模拟私有(private)方法的结果并同时获得声纳或 jacoco 的覆盖?

java - JList - 选择选项

java - Spring XML 中的 Apache Camel 路由定义并使用 SpringBoot 运行

java - Tomcat 上的 SpringBoot : Error creating bean with name 'jacksonObjectMapperBuilder'

Maven install :? - 如何创建候选发布版?

maven - 在 maven-plugin-testing-harness 中注入(inject) DefaultRepositorySystem 时出错

java - 由 : java. lang.AbstractMethodError : com. myapp.webapp.config.WebConfig$$EnhancerBySpringCGLIB$$b5f5a3c0.configureContentNegotiation 引起

java - ' *' as parameter changes to ".classpath, .project, .settings, bin, src,"