Jersey 与 Spring MVC 的集成Jersey、Spring、MVC

2023-09-06 15:27:52 作者:旧里迟暮

我想创建一个能够与 android 应用程序配合使用的网站.对于这些休息调用,我想使用单独的 servlet

I want to create a website that is able to work with rest with an android application. For these rest-calls I want to make use of a seperate servlet

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 时,它工作得很好.但是,当我将 @Autowired 与球衣一起使用时,我得到了一个空指针异常.

As you can see al the jersey information is passed using the url: /rest/* When I want to do a GET for a string using the url: localhost:9999/rest/user/sample it work perfectly. However when I use @Autowired with jersey I get a nullpointer exception.

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

}

有人发现问题了吗?

提前致谢.

推荐答案

使用 Spring Jersey 集成 servlet

Use Spring Jersey integration servlet

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