2013-04-25 104 views
1

我想創建一個能夠與android應用程序一起工作的網站。 對於這些剩餘的呼叫我想利用一個單獨的servletJersey與Spring MVC的集成

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" version="2.5"> 
<display-name>Stage XT-i</display-name> 


<welcome-file-list> 
    <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file> 
</welcome-file-list> 

<listener> 
    <listener-class> 
     org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener> 
<servlet> 
    <servlet-name>spring</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring-servlet.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>spring</servlet-name> 
    <url-pattern>*.html</url-pattern> 
</servlet-mapping> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/spring-servlet.xml 
     /WEB-INF/spring-security.xml 
    </param-value> 
</context-param> 


<!-- Spring Security --> 
<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

<servlet> 
    <servlet-name>Jersey Web Application</servlet-name> 
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
    <init-param> 
     <param-name>com.sun.jersey.config.property.packages</param-name> 
     <param-value>be.kdg.teamf</param-value> 
    </init-param> 
    <init-param> 
     <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> 
     <param-value>true</param-value> 
    </init-param> 
</servlet> 
<servlet-mapping> 
    <servlet-name>Jersey Web Application</servlet-name> 
    <url-pattern>/rest/*</url-pattern> 
</servlet-mapping> 
</web-app> 

正如你可以看到人的球衣信息使用的URL傳遞的:/ REST/* 當我想使用url:localhost:9999/rest/user/sample爲一個字符串做一個GET,它可以很好地工作。 但是,當我使用@澤西與澤西我得到一個空指針例外。

Java類:

@Path("/user") 
public class UserRest { 
@Autowired 
private UserService userService; 

@Context 
UriInfo uriInfo; 

// Another "injected" object. This allows us to use the information that's 
// part of any incoming request. 
// We could, for example, get header information, or the requestor's address. 
@Context 
Request request; 

// Basic "is the service running" test 
@GET 
@Produces(MediaType.TEXT_PLAIN) 
public String respondAsReady() { 
    return "Demo service is ready!"; 
} 

@GET 
@Path("sample") 
@Produces(MediaType.APPLICATION_JSON) 
public User getUserJson() { 
    User u = userService.listUsers().get(0); 
    System.out.println("Returning person"); 

    return u; 
} 

} 

有誰看到這個問題?

在此先感謝。

回答

2

使用Spring集成新澤西州的servlet

com.sun.jersey.spi.spring.container.servlet.SpringServlet 
+0

花費5小時面臨着同樣的問題後,這個固定 – samach 2015-07-18 20:02:41