【问题标题】:HTTP 404 with CXF (JAX-RS), Spring and Tomcat (no web.xml)带有 CXF (JAX-RS)、Spring 和 Tomcat(无 web.xml)的 HTTP 404
【发布时间】:2021-04-28 10:57:11
【问题描述】:

大家好,stackoverflow,

当我在 Eclipse 中通过“在服务器上运行”启动应用程序时,我得到了 HTTP 404。我的外部 Tomcat 是在 Eclipse 中配置的。

有人知道为什么吗?

非常感谢您的帮助。

KasInitializer.java

public class KasInitializer implements WebApplicationInitializer {

    private static final String SERVICE_PATH = "/I_Attachment_Services";

    @Override
    public void onStartup(ServletContext container) throws ServletException {

        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(AttachmentServicesConfig.class);

        container.addListener(new ContextLoaderListener(context));

        ServletRegistration.Dynamic dispatcher = container.addServlet("CXFServlet",
            new CXFServlet());
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping(SERVICE_PATH);
    }
}

AttachmentServicesApplication.java

@ApplicationPath("")
public class AttachmentServicesApplication extends Application {

}

AttachmentServicesConfig.java

@Configuration
@PropertySource("classpath:kas.properties")
public class AttachmentServicesConfig {

    @Bean(destroyMethod = "shutdown")
    public SpringBus cxf() {

        return new SpringBus();
    }

    @Bean
    public Server jaxRsServer() {

        JAXRSServerFactoryBean factory = RuntimeDelegate.getInstance()
            .createEndpoint(attachmentsApplication(), JAXRSServerFactoryBean.class);
        factory.setServiceBeans(
            Arrays.<Object>asList(attachmentsController(attachmentService(attachmentRepo()))));
        factory.setAddress(factory.getAddress());
        factory.setProviders(Arrays.<Object>asList(jsonProvider()));
        return factory.create();
    }

    @Bean
    public AttachmentServicesApplication attachmentsApplication() {

        return new AttachmentServicesApplication();
    }

    @Bean
    public JacksonJsonProvider jsonProvider() {

        return new JacksonJsonProvider();
    }


    @Bean
    public AttachmentREST attachmentsController(AttachmentService attachmentService) {

        return new AttachmentRESTImpl(attachmentService);
    }

    @Bean
    public AttachmentService attachmentService(AttachmentFileStorage repo) {

        return new AttachmentServiceImpl(repo);
    }

    @Bean
    public AttachmentFileStorage attachmentRepo() {

        return new AttachmentFileStorageImpl();
    }
}

AttachmentREST.java

@Path("/")
public interface AttachmentREST {

    @GET
    @Path("/MaxMailSize")
    @Produces({ MediaType.TEXT_PLAIN })
    Response readMaxMailSize();
    
    ...
}

pom.xml

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

<parent>
    <groupId>com.cgm.de.connectivity.ti.kim</groupId>
    <artifactId>kas</artifactId>
    <version>11.0.0-0-SNAPSHOT</version>
</parent>

<artifactId>kas-server</artifactId>
<packaging>war</packaging>

<build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.2.3</version>
            <configuration>
                <warName>I_Attachment_Services</warName>
                <warSourceDirectory>WebContent</warSourceDirectory>
            </configuration>
        </plugin>
        <!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> 
            <version>3.2.0</version> <configuration> <archive> <manifest> <mainclass>com.cgm.de.connectivity.ti.kim.kas.server.KASServer</mainclass> 
            </manifest> </archive> </configuration> </plugin> -->
    </plugins>
</build>

<dependencies>
    <!-- === inherited dependencies === -->

    <!-- kas-core -->
    <dependency>
        <groupId> ${project.groupId}</groupId>
        <artifactId>kas-core</artifactId>
    </dependency>

    <!-- commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
    </dependency>

    <!-- CXF -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxrs</artifactId>
    </dependency>

    <!-- === project-specific dependencies === -->

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.3.4</version>
    </dependency>

    <!-- JSON -->
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.12.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-base</artifactId>
        <version>2.12.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.12.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.12.2</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
        <version>3.0.1</version>
    </dependency>

    <!-- javax.servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
</dependencies></project>

控制台输出:

Apr 28, 2021 1:32:49 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Server version:        Apache Tomcat/8.5.35
Apr 28, 2021 1:32:49 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Server built:          Nov 3 2018 17:39:20 UTC
Apr 28, 2021 1:32:49 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Server number:         8.5.35.0
Apr 28, 2021 1:32:49 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: OS Name:               Linux
Apr 28, 2021 1:32:49 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: OS Version:            5.9.12-050912-generic
Apr 28, 2021 1:32:49 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Architecture:          amd64
Apr 28, 2021 1:32:49 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Java Home:             /home/pepe/.sdkman/candidates/java/8.0.265-zulu/jre
Apr 28, 2021 1:32:49 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: JVM Version:           1.8.0_265-b11
Apr 28, 2021 1:32:49 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: JVM Vendor:            Azul Systems, Inc.
Apr 28, 2021 1:32:49 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: CATALINA_BASE:         /home/pepe/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Apr 28, 2021 1:32:49 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: CATALINA_HOME:         /home/pepe/tools/server/tomcat/apache-tomcat-8.5.35
Apr 28, 2021 1:32:49 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dcatalina.base=/home/pepe/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Apr 28, 2021 1:32:49 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dcatalina.home=/home/pepe/tools/server/tomcat/apache-tomcat-8.5.35
Apr 28, 2021 1:32:49 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dwtp.deploy=/home/pepe/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
Apr 28, 2021 1:32:49 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Djava.endorsed.dirs=/home/pepe/tools/server/tomcat/apache-tomcat-8.5.35/endorsed
Apr 28, 2021 1:32:49 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dfile.encoding=UTF-8
Apr 28, 2021 1:32:49 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFORMATION: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
Apr 28, 2021 1:32:49 PM org.apache.coyote.AbstractProtocol init
INFORMATION: Initializing ProtocolHandler ["http-nio-8080"]
Apr 28, 2021 1:32:49 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFORMATION: Using a shared selector for servlet write/read
Apr 28, 2021 1:32:49 PM org.apache.coyote.AbstractProtocol init
INFORMATION: Initializing ProtocolHandler ["ajp-nio-8009"]
Apr 28, 2021 1:32:49 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFORMATION: Using a shared selector for servlet write/read
Apr 28, 2021 1:32:49 PM org.apache.catalina.startup.Catalina load
INFORMATION: Initialization processed in 525 ms
Apr 28, 2021 1:32:49 PM org.apache.catalina.core.StandardService startInternal
INFORMATION: Starting service [Catalina]
Apr 28, 2021 1:32:49 PM org.apache.catalina.core.StandardEngine startInternal
INFORMATION: Starting Servlet Engine: Apache Tomcat/8.5.35
Apr 28, 2021 1:32:50 PM org.apache.jasper.servlet.TldScanner scanJars
INFORMATION: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Apr 28, 2021 1:32:50 PM org.apache.catalina.core.ApplicationContext log
INFORMATION: No Spring WebApplicationInitializer types detected on classpath
Apr 28, 2021 1:32:51 PM org.apache.catalina.core.ApplicationContext log
INFORMATION: 1 Spring WebApplicationInitializers detected on classpath
Apr 28, 2021 1:32:51 PM org.apache.jasper.servlet.TldScanner scanJars
INFORMATION: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Apr 28, 2021 1:32:51 PM org.apache.catalina.core.ApplicationContext log
INFORMATION: Initializing Spring root WebApplicationContext
13:32:51.719 [localhost-startStop-1] INFO  org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
13:32:52.298 [localhost-startStop-1] INFO  org.apache.cxf.endpoint.ServerImpl - Setting the server's publish address to be /
13:32:52.374 [localhost-startStop-1] INFO  org.springframework.web.context.ContextLoader - Root WebApplicationContext initialized in 651 ms
Apr 28, 2021 1:32:52 PM org.apache.coyote.AbstractProtocol start
INFORMATION: Starting ProtocolHandler ["http-nio-8080"]
Apr 28, 2021 1:32:52 PM org.apache.coyote.AbstractProtocol start
INFORMATION: Starting ProtocolHandler ["ajp-nio-8009"]
Apr 28, 2021 1:32:52 PM org.apache.catalina.startup.Catalina start
INFORMATION: Server startup in 2839 ms

编辑:

当我手动将war文件复制到../tomcat/webapps文件夹并像“./catalina.sh run”一样启动tomcat时,我在localhost.2021-04-28.log中得到以下行:

28-Apr-2021 15:15:12.912 INFORMATION [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log 1 Spring WebApplicationInitializers detected on classpath
28-Apr-2021 15:15:13.315 INFORMATION [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log Initializing Spring root WebApplicationContext
28-Apr-2021 15:15:14.170 INFORMATION [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log ContextListener: contextInitialized()
28-Apr-2021 15:15:14.170 INFORMATION [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log SessionListener: contextInitialized()
28-Apr-2021 15:15:14.172 INFORMATION [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log ContextListener: attributeAdded('StockTicker', 'async.Stockticker@491ac41a')

但是在 localhost_access_log.2021-04-28.txt:

0:0:0:0:0:0:0:1 - - [28/Apr/2021:15:15:48 +0200] "GET /I_Attachment_Services/MaxMailSize HTTP/1.1" 404 1111

编辑 2:

当我在嵌入式 Tomcat 上部署相同的应用程序时,一切正常:

public class KasTomcatServer {

    private static final String SERVICE_PATH = "/I_Attachment_Services/*";
    private static final String CONTEXT_PATH = "/";
    private static final int PORT = 8080;

    public static void main(String[] args) throws Exception {

        final File base = createBaseDirectory();

        final Tomcat tomcat = new Tomcat();
        tomcat.setPort(PORT);
        tomcat.setBaseDir(base.getAbsolutePath());

        Context context = tomcat.addContext(CONTEXT_PATH, base.getAbsolutePath());
        Tomcat.addServlet(context, "CXFServlet", new CXFServlet());

        context.addServletMapping(SERVICE_PATH, "CXFServlet");
        context.addApplicationListener(ContextLoaderListener.class.getName());
        context.setLoader(new WebappLoader(Thread.currentThread().getContextClassLoader()));

        context.addParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName());
        context.addParameter("contextConfigLocation", AttachmentServicesConfig.class.getName());

        TomcatURLStreamHandlerFactory.disable();

        tomcat.start();
        tomcat.getServer().await();
    }

    private static File createBaseDirectory() throws IOException {

        final File base = File.createTempFile("tmp-", "");

        if (!base.delete()) {
            throw new IOException(
                "Base directory couldn't be deleted: " + base.getAbsolutePath());
        }

        if (!base.mkdir()) {
            throw new IOException(
                "Base directory couldn't be created: " + base.getAbsolutePath());
        }

        return base;
    }

}

【问题讨论】:

  • 您使用的是哪个 Tomcat 版本? Jersey 3 需要 Tomcat 10,而 Spring 需要 Tomcat 9.0。
  • @PiotrP.Karwasz v8.5.35
  • @PiotrP.Karwasz 我需要球衣吗?在 POM 中没有任何 Jersey 依赖项的情况下启动 tomcat 时,我没有收到任何错误日志。
  • 如果您使用的是 CXF,则不需要 Jersey。
  • 不,你不需要 Jersey,CXF 有一个 JAX-RS 的实现。

标签: java xml maven tomcat cxf


【解决方案1】:

您为您的 CXFServlet 提供了过于严格的映射(参见 MappingMatch 的映射类型):

  • 您使用的精确映射 /I_Attachment_Services/I_Attachment_Services 之外的任何内容都不匹配,
  • 您需要一个前缀映射 /I_Attachment_Services/* 匹配 /I_Attachment_Services 以及以 /I_Attachment_Services/ 开头的所有内容。

只需将常量SERVICE_PATH 更改为/I_Attachment_Services/*

备注:行:

factory.setAddress(factory.getAddress());

没有意义。你可能想要factory.setAddress(null) 或类似的东西。

【讨论】:

  • 我按照您的建议更改了常量和 factory.setAddress 行,但响应仍然相同:HTTP 404
  • 只是为了确保:您是否在请求的 URI 路径前加上上下文路径?您在问题中提到了GET /I_Attachment_Services/MaxMailSize HTTP/1.1,但这仅在您使用空路径部署应用程序时才有效(或调用WAR 文件ROOT.war)。在 Eclipse 中,您可能有不同的上下文路径。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-06
  • 2017-09-08
  • 1970-01-01
  • 2014-05-31
  • 1970-01-01
相关资源
最近更新 更多