java - 使用 JBoss 部署 REST 服务时出现的问题

标签 java rest spring-mvc jboss war

我必须创建一个 REST 服务并将其部署在 JBoss 服务器上。 现在它只是用“hello”字符串响应任何 POST 请求(我只需要测试至少这是否有效)。

由于技术限制,我不得不使用 jdk 1.7。 它使用 Java、Maven 和 Spring MVC 进行编码。

据我所知,该代码应该可以工作。 war 已成功部署,根据 jboss 的日志,我认为它正在按照我的预期映射资源。问题是,当我向我期望的 URI (X.X.X.X:8080/sduca/OSSreceiver) 发送 POST 请求时,找不到资源。

我真的找不到原因或方法。我假设我从来没有做过这样的事情,但根据同事的说法,它应该有效。可能是什么原因?我错过了什么吗?

抱歉,如果我把整个项目放在一起(我讨厌是谁做的),但我认为这是必要的,因为我输入的每一行都可能是愚蠢的。

无论如何,谢谢! :)

我得到的回复

JBWEB000065: HTTP Status 404 - /sduca/OSSreceiver

JBWEB000309: type JBWEB000067: Status report

JBWEB000068: message /sduca/OSSreceiver

JBWEB000069: description JBWEB000124: The requested resource is not available.

这是我部署时写入的日志:

[0m[0m15:12:53,106 INFO  [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 2) JBAS015009: Scan found incompletely copied file content for deployment /opt/UTM/jboss/standalone/deployments/sduca-consumer-listener-1.0-SNAPSHOT.war. Deployment changes will not be processed until all content is complete.
[0m[0m15:13:02,076 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-7) JBAS015876: Starting deployment of "sduca-consumer-listener-1.0-SNAPSHOT.war" (runtime-name: "sduca-consumer-listener-1.0-SNAPSHOT.war")
[0m[33m15:13:02,888 WARN  [org.jboss.as.ee] (MSC service thread 1-11) JBAS011006: Not installing optional component org.springframework.http.server.ServletServerHttpAsyncRequestControl due to an exception (enable DEBUG log level to see the cause)
[0m[33m15:13:02,889 WARN  [org.jboss.as.ee] (MSC service thread 1-11) JBAS011006: Not installing optional component org.springframework.web.context.request.async.StandardServletAsyncWebRequest due to an exception (enable DEBUG log level to see the cause)
[0m[0m15:13:02,926 INFO  [org.jboss.web] (ServerService Thread Pool -- 219) JBAS018210: Register web context: /sduca
[0m[0m15:13:02,945 INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/sduca]] (ServerService Thread Pool -- 219) 1 Spring WebApplicationInitializers detected on classpath
[0m[0m15:13:03,005 INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/sduca]] (ServerService Thread Pool -- 219) Initializing Spring root WebApplicationContext
[0m[0m15:13:03,006 INFO  [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 219) Root WebApplicationContext: initialization started
[0m[0m15:13:03,024 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (ServerService Thread Pool -- 219) Refreshing Root WebApplicationContext: startup date [Thu Aug 30 15:13:03 CEST 2018]; root of context hierarchy
[0m[0m15:13:03,100 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (ServerService Thread Pool -- 219) Registering annotated classes: [class com.hpe.oss.eventhandler.sduca.receiver.config.AppConfig]
[0m[0m15:13:03,483 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (ServerService Thread Pool -- 219) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
[0m[0m15:13:03,809 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 219) Mapped "{[/OSSreceiver],methods=[POST]}" onto public java.lang.String com.hpe.oss.eventhandler.sduca.receiver.OSSreceiver.postResponse()
[0m[0m15:13:04,071 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] (ServerService Thread Pool -- 219) Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Thu Aug 30 15:13:03 CEST 2018]; root of context hierarchy
[0m[0m15:13:04,188 INFO  [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 219) Root WebApplicationContext: initialization completed in 1182 ms
[0m[0m15:13:04,189 INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/sduca]] (ServerService Thread Pool -- 219) Initializing Spring FrameworkServlet 'dispatcher'
[0m[0m15:13:04,189 INFO  [org.springframework.web.servlet.DispatcherServlet] (ServerService Thread Pool -- 219) FrameworkServlet 'dispatcher': initialization started
[0m[0m15:13:04,191 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (ServerService Thread Pool -- 219) Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Thu Aug 30 15:13:04 CEST 2018]; parent: Root WebApplicationContext
[0m[0m15:13:04,192 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (ServerService Thread Pool -- 219) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
[0m[0m15:13:04,220 INFO  [org.springframework.web.servlet.DispatcherServlet] (ServerService Thread Pool -- 219) FrameworkServlet 'dispatcher': initialization completed in 31 ms
[0m[0m15:13:04,239 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018559: Deployed "sduca-consumer-listener-1.0-SNAPSHOT.war" (runtime-name : "sduca-consumer-listener-1.0-SNAPSHOT.war")

这是 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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>sduca-consumer-listener</groupId>
    <artifactId>sduca-consumer-listener</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <sourceDirectory>src</sourceDirectory>
        <!-- Telling to use jdk 1.7 -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <!-- I am not using web.xml -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <webappDirectory>/sduca</webappDirectory>
                </configuration>
            </plugin>

        </plugins>
    </build>

    <dependencies>
        <!-- Using Spring 4 because of jdk 1.7 -->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.18.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>


</project>

这个是src/main/webapp/WEB-INF/jboss-web.xml的内容

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
    <context-root>sduca</context-root>
</jboss-web>

一共有三个类。 应用程序初始化程序.java /* 这个类配置一些东西的servlet */ 包 com.hpe.oss.eventhandler.sduca.receiver.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{ AppConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

AppConfig.java

/*
This class is just for configuration and replace web.xml
Which is an old way of configuring I guess
 */

package com.hpe.oss.eventhandler.sduca.receiver.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.hpe.oss.eventhandler.sduca.receiver")
public class AppConfig {
}

Controller :OSSreceiver.java

package com.hpe.oss.eventhandler.sduca.receiver;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class OSSreceiver {

    @RequestMapping(value="/OSSreceiver", method = RequestMethod.POST)
    public @ResponseBody String postResponse()
    {
        return "hello";
    }
}

最佳答案

将映射更改为 /*/sduca/*

  @Override
  protected String[] getServletMappings() {
    return new String[]{"/*"};
  }

关于java - 使用 JBoss 部署 REST 服务时出现的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52101285/

相关文章:

javascript - AngularJS:操作 `query` 的资源配置错误。预期响应包含一个对象,但得到一个数组

Python - Flask Api 异常

通过引用链: java. util.HashMap出现java.lang.UnsupportedOperationException

security - 确保休息服务

java - 如何转换一串俄语西里尔字母?

java - Spring HandlerMethodSelector.selectMethods 的替代品

java - 将 Controller 中的方法链接到 Spring boot Java 中的按钮单击

java - Spring Autowiring 无法看到上下文 bean,除非在基础包上定义了组件扫描

java - 不使用任何第三方库在Web上登录twitter的指南

java - Tomcat7卡塔琳娜错误