2015-10-06 165 views
0

好的,我使用的是jersey1,一切正常。它運行在java8下,我需要添加spring bean。由於只有spring4工作java 8和只有jersey2支持spring4我遷移到他們。我找到this作爲示例,並且更改了pom.xmlweb.xml一點。現在我得到了404.所有事情都很好,所以我110%確定我做的是正確的鏈接。 localhost:8080/public/myservice/login。以下是文件:404將球衣1換成球衣2後

的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

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

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

<?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啓動日誌(碼頭打印漂亮相同)

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

回答

0

最後由春天去除這部分

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

和更換有這個工作 @ApplictaionPath@Path in servle t類。