Jersey与Spring MVC的集成 [英] Jersey integration with Spring MVC

查看:94
本文介绍了Jersey与Spring MVC的集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个可以与其他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>

如您所见,球衣信息是使用以下网址传递的:/rest/* 当我想使用url进行字符串的GET:localhost:9999/rest/user/sample时,它可以完美地工作. 但是,当我在球衣上使用@Autowired时,会出现nullpointer异常.

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

这篇关于Jersey与Spring MVC的集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆