简体   繁体   English

在 Jboss 上从 STS(Spring Tool Suite)3 部署 WAR,返回 404

[英]Deployed WAR from STS (Spring Tool Suite) 3 on Jboss returning 404

I'm trying to deploy a spring boot app into jboss using STS with the JBoss tools plugin.我正在尝试使用带有 JBoss 工具插件的 STS 将 Spring Boot 应用程序部署到 jboss 中。

The application runs fine if I run it as "Spring Boot App", but it returns 404 when I run it as "Server" on a Red Hat 7.2 using the EAP 7.2 with JBoss tolls on STS.如果我将其作为“Spring Boot App”运行,该应用程序运行良好,但当我在 Red Hat 7.2 上使用 EAP 7.2 和 STS 上的 JBoss 收费将其作为“服务器”运行时,它会返回 404。

The version of STS I'm using is 3.9.8.RELEASE The version of the JBoss Tools plugin is 3.10.2.v20181101-1129我使用的STS版本是3.9.8.RELEASE JBoss Tools插件的版本是3.10.2.v20181101-1129

This is my POM file:这是我的 POM 文件:

<?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>

    <groupId>org.springframework</groupId>
    <artifactId>gs-rest-service</artifactId>
    <version>0.1.0</version>
    <packaging>war</packaging>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>


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

</project>

This is the override for the SpringApplicationBuilder:这是 SpringApplicationBuilder 的覆盖:

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class SpringInitializer  extends SpringBootServletInitializer{

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

This is my Controller class:这是我的控制器类:

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }
}

And this is the main class:这是主要课程:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

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

And this is the greeting class....just in case:这是问候语……以防万一:

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

If I right click on the project explorer, run as Spring Boot App, the application returns the right response when called like this:如果我右键单击项目资源管理器,作为 Spring Boot App 运行,应用程序在调用时返回正确的响应,如下所示:

http://localhost:8080/greeting http://localhost:8080/问候

Response when running as Spring Boot App作为 Spring Boot App 运行时的响应

but if I deploy it to Jboss it returns 404.但如果我将它部署到 Jboss,它会返回 404。

When I run it as Server, the logs return this:当我将它作为服务器运行时,日志会返回:

17:48:15,176 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 9) Inicializando Mojarra 2.3.5.SP2-redhat-00001 para el contexto '/gs-rest-service-0.1.0'
17:48:15,590 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 9) WFLYUT0021: Contexto de Web registrado: '/gs-rest-service-0.1.0' para el servidor 'default-server'
17:48:15,609 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) WFLYSRV0010: Implementado "gs-rest-service-0.1.0.war" (runtime-name : "gs-rest-service-0.1.0.war")

Which I assume, changes my context to: http://localhost:8080/gs-rest-service-0.1.0/greeting我假设,将我的上下文更改为:http://localhost:8080/gs-rest-service-0.1.0/greeting

Response when running as Server on Jboss 7.2 EAP在 Jboss 7.2 EAP 上作为服务器运行时的响应

There are some steps you need to do when you want to deploy format change from jar to war.当您想要部署从 jar 到 war 的格式更改时,您需要执行一些步骤。

1. You need to make your main method extends "SpringBootServletInitializer". 1.您需要使您的主要方法扩展“SpringBootServletInitializer”。

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

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

Please check the newly implemented "configure" method.请检查新实现的“配置”方法。

2. Change the scope of embed servlet. 2.改变embed servlet的范围。

Once you start using war, you don't need to use default tomcat in springboot .一旦你开始使用 war,你就不需要在 springboot 中使用默认的 tomcat 了 Open your pom.xml file and change & add this content.打开您的 pom.xml 文件并更改并添加此内容。

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

You need to check "scope" is set "provided" .您需要检查“范围”是否设置为“提供”

3. Package in "war". 3.包在“战争”中。

Finally, you need to package your application in war file.最后,您需要将应用程序打包到 war 文件中。 Please open your pom.xml and change or add "packaging" value.请打开您的 pom.xml 并更改或添加“包装”值。

<packaging>war</packaging>

I hope my answer will help you.希望我的回答对你有所帮助。 Have a nice day & coding!祝你有美好的一天和编码!

While deploying the spring boot war file in JBoss EAP you need to take care of few things like below.在 JBoss EAP 中部署 spring boot war 文件时,您需要注意以下几点。

You need to create jboss-deployment-structure.xml file inside src/main/webapp/WEB-INF folder.您需要在 src/main/webapp/WEB-INF 文件夹中创建 jboss-deployment-structure.xml 文件。

    <jboss-deployment-structure>
    <deployment> 
        <exclude-subsystems>
            <subsystem name="jpa" />
        </exclude-subsystems>
         <?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example.com</groupId>
    <artifactId>projectname</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>

    <name>projectname</name>
    <description>description</description>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </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-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin> 
        </plugins>
    </build>
</project>
        <exclusions>
            <module name="org.jboss.logging" /> 
            <module name="javaee.api" /> 
        </exclusions>  
    </deployment>
</jboss-deployment-structure>  

After that create jboss-web.xml file in directory src/main/webapp/WEB-INF as mentioned below.之后在目录 src/main/webapp/WEB-INF 中创建 jboss-web.xml 文件,如下所述。

<jboss-web>
     <context-root>appname</context-root>
</jboss-web>

After doing these changes build the project using "mvn clean install" and them locate the war file in the target folder and deploy that war file on JBoss EAP.完成这些更改后,使用“mvn clean install”构建项目,然后他们在目标文件夹中找到war 文件并将该war 文件部署到JBoss EAP。

Try to access the deployed war file using URL below, don't give the snapshot and version in the context.尝试使用下面的 URL 访问部署的战争文件,不要在上下文中提供快照和版本。

http://localhost:8080/gs-rest-service/greeting http://localhost:8080/gs-rest-service/greeting

I hope this will help.我希望这将有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM