如何将Spring项目与Jersey集成?

时间:2015-08-05 12:08:23

标签: java spring-security jersey-2.0

我要做的是将一个简单的Spring项目与Jersey集成。 Spring上的Web部分工作得很好。泽西岛的问题。我的端点不起作用。当我通过REST客户端发送请求时,我得到HTTP 200 No Error并且在响应正文中我得到一个HTML登录表单,如果我通过浏览器向REST发送请求,它会返回404 Not Found

经过一些调试后,我发现Jersey的端点方法没有调用。

这是我的安全配置:

<security:http auto-config="true" use-expressions="true">
    <security:access-denied-handler error-page="/login"/>
    <security:intercept-url pattern="/j_spring_security_check" access="isAnonymous()"/>
    <security:intercept-url pattern="/login" access="isAnonymous()"/>
    <security:intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>

    <security:form-login
            login-page='/login'
            default-target-url="/"
            authentication-failure-url="/login?error=true"/>

    <security:logout
            invalidate-session="true"
            logout-success-url="/login?logout=true"
            logout-url="/j_spring_security_logout"/>

    <security:remember-me/>
    <security:csrf disabled="false"/>
</security:http>

<security:authentication-manager>
    <security:authentication-provider>
        <security:user-service>
            <security:user
                    name="Jack"
                    password="secret"
                    authorities="ROLE_USER"/>
        </security:user-service>
    </security:authentication-provider>
</security:authentication-manager>

我的web.xml配置:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/dispatcherServlet.xml
        /WEB-INF/securityContext.xml
    </param-value>
</context-param>
<servlet>
    <servlet-name>jersey-servlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>ua.alex.csrf.poc.rest.JaxRsApplication</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>        
</filter>
<servlet-mapping>
    <servlet-name>jersey-servlet</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcherServlet.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

我的调度程序servlet看起来像这样

<mvc:annotation-driven/>
<context:component-scan base-package="de.miho.rest.test"/>

<mvc:resources mapping="/static/**" location="/static/" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

My Jersey应用程序如下所示:

public class JaxRsApplication extends ResourceConfig {

    public JaxRsApplication() {
        register(RequestContextFilter.class);
        register(MyResource.class);
        register(JacksonFeature.class);
    }
}

My Jersey EndPoint

@Component
@Path("/api")
public class MyResource {

    @Autowired
    private UserService service;

    @GET
    @Path("/greeting")
    @Produces(MediaType.APPLICATION_JSON)
    public Response hello1() {
        String userName = service.getUserName();
        return Response.ok("Hello, " + userName).build();
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response hello2() {
        return Response.ok("Hello again!").build();
    }
}

我该如何解决这个问题?

0 个答案:

没有答案