春季启动战不适用于EAP

春季启动战不适用于EAP

本文介绍了春季启动战不适用于EAP 6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Spring Boot创建了一个基于REST的小型应用程序.该应用程序作为war软件包部署在EAP 6(JBoss)上.由于EAP 6基于Java 1.7,因此我已经在maven pom中将其配置为编译和使用Java 1.7版本.当我部署应用程序时,我可以在服务器日志中看到该控制器正在注册,但是当我点击它时却得到了404.此外,我JBoss并没有选择我的上下文根配置,而是将应用程序名称作为上下文根.我已经测试了所有可能的端点,但是所有东西都给出了404.有人可以建议我一些可以帮助我继续前进的东西吗?

I have created a small REST-based application using spring boot. The application is deployed on EAP 6(JBoss) as a war package.As EAP 6 is based on Java 1.7 I have configured that in my maven pom to compile and use Java 1.7 version.When I am deploying the application I can see in the server logs that the controller is getting registered but when I am hitting it I am getting 404. Also I JBoss is not picking up my context root configuration but taking the application name as the context root. I am tested all the possible endpoints but everything is giving 404.Can someone suggest me something which can help me to proceed forward?

POM文件:

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.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.7</java.version>
</properties>

....

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

应用程序配置

package com.org.orderhistory.v2.orderhistory.v2;
import ...
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

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

}

package com.org.orderhistory.v2.orderhistory.v2.controllers;

import ...

@RestController
@RequestMapping(value="/myorder/weborders")
public class WebOrderControllers {

@RequestMapping(value="/{webUserId}",method = RequestMethod.GET, produces = "application/json")
public List<WebOrder> getWebOrdersForUser(@PathVariable Long webUserId) {

JBoss日志

2017-10-09 02:24:29,744 [ServerService Thread Pool -- 594] INFO  [org.springframework.boot.web.servlet.FilterRegistrationBean] Mapping filter: 'requestContextFilter' to: [/*]
2017-10-09 02:24:30,368 [ServerService Thread Pool -- 594] INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5a46b924: startup date [Mon Oct 09 02:24:27 EDT 2017]; root of context hierarchy
2017-10-09 02:24:30,451 [ServerService Thread Pool -- 594] INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] Mapped "{[/org/weborders/{webUserId}],methods=[GET],produces=[application/json]}" onto public java.util.List<com.org.www.order.model.WebOrder> com.org.orderhistory.v2.orderhistory.v2.controllers.WebOrderControllers.getWebOrdersForUser(java.lang.Long)

推荐答案

此处完全相同.试试这个:

1.在pom.xml中:

Exact same issue here. Try this:

1. In pom.xml:

<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <scope>provided</scope>
</dependency>

2.添加一个实现WebApplicationInitializer的类:

2. Add a class implements WebApplicationInitializer:

@Configuration
public class WebApplicationInitializerImpl implements WebApplicationInitializer{

    @Override
    public void onStartup(ServletContext container) throws ServletException {
        WebApplicationContext context = getContext();

        Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(context));
        registration.setLoadOnStartup(1);
        registration.addMapping("/*");
    }

    private WebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation(ApplicationMain.class.getName());
        return context;
    }

}

3.记住要通过您的应用程序扩展SpringBootServletInitializer:

3. Remember to extend SpringBootServletInitializer by your application:

@SpringBootApplication
public class ApplicationMain extends SpringBootServletInitializer{

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

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

如果有效,则问题与dispatcherServlet有关.
使用Spring boot时,应该已经自动配置了dispatcherServlet. (这是自动配置的一部分)
根据Redhat的解释,这种情况似乎是jboss的问题:

If it works, then the problem is related to dispatcherServlet.
When using Spring boot, a dispatcherServlet should have been configured automatically. (That is a part of auto Configuration)
And this situation seems to be the issue of jboss, according to Redhat's explanation:

要检查您的dispatcherServlet是否按预期注册,请尝试以下操作:

To check if your dispatcherServlet is registered as expected, try this:

[standalone@localhost:9999 /] /deployment=spring-boot-application.war/subsystem=web:read-resource(recursive=true)

不起作用:

{
    "outcome" => "success",
    "result" => {
        "context-root" => "/spring-boot-application",
        "servlet" => undefined,
        "virtual-host" => "default-host"
    }
}

工作:

{
    "outcome" => "success",
    "result" => {
        "context-root" => "/spring-boot-application",
        "virtual-host" => "default-host",
        "servlet" => {"appServlet" => {
            "servlet-class" => "org.springframework.web.servlet.DispatcherServlet",
            "servlet-name" => "appServlet"
        }}
    }
}

希望它可以解决您的问题.

参考:
https://access.redhat.com/solutions/1211203
https://blog.csdn.net/u011160656/article/details/78809239

Hope it solves your problem.

Reference:
https://access.redhat.com/solutions/1211203
https://blog.csdn.net/u011160656/article/details/78809239

这篇关于春季启动战不适用于EAP 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 04:03