简体   繁体   English

将jersey1更改为jersey2后的404

[英]404 after changing jersey1 to jersey2

Ok, I was using jersey1 and everything worked fine. 好的,我使用的是jersey1 ,一切正常。 It's running under java8 and I needed to add spring beans. 它在java8下运行,我需要添加spring bean。 Since only spring4 works java 8 and only jersey2 supports spring4 i migrated to them. 由于只有spring4可以使用java 8 ,只有jersey2支持spring4我迁移到了它们。 I found this post as a sample and changed pom.xml and web.xml a bit. 我发现帖子是一个示例,并稍微更改了pom.xmlweb.xml Now I'm getting 404. Everything worked fine before so I'm 110% sure I do the right link. 现在我得到404。之前一切正常,因此我有110%的把握确定了正确的链接。 localhost:8080/public/myservice/login . localhost:8080/public/myservice/login Here are the files: 这些是文件:

pom.xml 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">

    <properties>
        <jersey.version>2.7</jersey.version>
    </properties>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>myid</artifactId>
    <packaging>war</packaging>
    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <!-- if Servlet API older than 3.0, use "jersey-container-servlet-core"  -->
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-spring3</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>2.7</version>
        </dependency>

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

    </dependencies>
</project>

web.xml web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app ....>

    <display-name>RestDemo</display-name>
    <description>RestDemo</description>

    <servlet>
        <servlet-name>public</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>package.that.contains.my.class</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>public</servlet-name>
        <url-pattern>/public/*</url-pattern>
    </servlet-mapping>

</web-app>

MyClass.java MyClass.java

package package.that.contains.my.class

import javax.ws.rs.*;
import javax.ws.rs.ApplicationPath;

@Component
@ApplicationPath("/myservice")
public class WebServiceWrapper extends javax.ws.rs.core.Application {

    @POST
    @Path("/login")
    @Produces(MediaType.APPLICATION_JSON)
    public void userLogin() throws IOException {
       //
    }

applicationContext.xml applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="....">
   <bean id="appConfigProperties"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>

    <context:component-scan base-package="package.that.contains.my.class"/>

</beans>

Tomcat startup logs (jetty print pretty the same) Tomcat启动日志(jetty打印几乎相同)

[2015-10-06 05:22:55,279] Artifact wrapper:war exploded: Artifact is being deployed, please wait...
Oct 06, 2015 5:22:55 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing Root WebApplicationContext: startup date [Tue Oct 06 17:10:16 EEST 2015]; root of context hierarchy
Oct 06, 2015 5:22:57 PM org.glassfish.jersey.servlet.init.JerseyServletContainerInitializer addServletWithApplication
INFO: Registering the Jersey servlet application, named package.that.contains.my.class.WebServiceWrapper, at the servlet mapping /myservice/*, with the Application class of the same name.
Oct 06, 2015 5:22:57 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Oct 06, 2015 5:22:57 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Tue Oct 06 17:22:57 EEST 2015]; root of context hierarchy
Oct 06, 2015 5:22:57 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Oct 06, 2015 5:22:57 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
Oct 06, 2015 5:22:58 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 711 ms
Oct 06, 2015 5:22:58 PM org.glassfish.jersey.server.ApplicationHandler initialize
INFO: Initiating Jersey application, version Jersey: 2.7 2014-03-12 18:11:31...
Oct 06, 2015 5:22:58 PM org.glassfish.jersey.server.ApplicationHandler initialize
INFO: Initiating Jersey application, version Jersey: 2.7 2014-03-12 18:11:31...
Oct 06, 2015 5:22:58 PM org.glassfish.jersey.server.ApplicationHandler initialize
INFO: Initiating Jersey application, version Jersey: 2.7 2014-03-12 18:11:31...

Finally got this work by removing this part from spring 终于通过从弹簧上拆下了这部分来完成了这项工作

<bean id="appConfigProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>

And replacing @ApplictaionPath to @Path in servlet class. 并将@ApplictaionPath替换为Servlet类中的@Path

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

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