简体   繁体   English

Jersey REST服务作为Spring Component

[英]Jersey REST service as Spring Component

I hava a rest service inside my app and i would like it to use spring beans. 我在我的应用程序内有一个休息服务,我希望它使用春豆。 Spring i am using is version 2.5. 我正在使用的是版本2.5。 Part of web.xml: web.xml的一部分:

<servlet>
    <servlet-name>jersey-servlet</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>com.app.rest</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

In pom.xml i have dependencies: 在pom.xml中我有依赖项:

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.8</version>
</dependency>
<dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.8</version>
</dependency>

In context.xml i have added: 在context.xml中我添加了:

<context:component-scan base-package="com.app.rest"/>

and also: 并且:

<tx:annotation-driven proxy-target-class="true" />

Interesting part of service class: 有趣的服务类部分:

@Component
@Path("/notification")
@Scope("singleton")
public class PushNotification {

    @Autowired
    //@Resource(name = "sendMessageServiceFacade")
    private SendMessageServiceFacade sendMessageServiceFacade;

    @Autowired
    //@Resource(name = "responseCode")
    private NotificationResponseMessage responseCode;

    @Autowired
    //@Resource(name = "validator")
    private PushMessagePreValidator validator;

    @POST
    @Path("/register")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response registerDevice(PushWooshRegisterDeviceDto dto) {
        if (validator.validate(dto) == 200) {
            return invokePushMessage(dto, PushMessageType.REGISTER.getMsgType());
        } else {
            return buildFailResponse();
        }
    }

    @GET
    @Path("/{param}")
    public Response ping(@PathParam("param") String param) {
        return Response.status(200).entity(param).build();
    }

GET method works as expected (basicly it works :) ), but POST method throws NullPointerException on line: if (validator.validate(dto) == 200). GET方法按预期工作(基本上它工作:)),但POST方法在行上抛出NullPointerException:if(validator.validate(dto)== 200)。

I dont know why @Autowired annotation doesnt work here, ale i tried with @Resource - same effect. 我不知道为什么@Autowired注释在这里不起作用,我尝试使用@Resource - 同样的效果。

Any tip may help. 任何提示可能有所帮助

Thanks in advance, Marek. 提前谢谢,马雷克。

If you want to use Jersey with Spring, you need to utilize the Spring integration module - ie depend on jersey-spring and use SpringServlet instead of Jersey ServletContainer servlet class. 如果你想使用Jersey和Spring,你需要使用Spring集成模块 - 即依赖于jersey-spring并使用SpringServlet而不是Jersey ServletContainer servlet类。 See here for more info: http://jersey.java.net/nonav/apidocs/latest/contribs/jersey-spring/com/sun/jersey/spi/spring/container/servlet/package-summary.html 有关详细信息,请参阅此处: http//jersey.java.net/nonav/apidocs/latest/contribs/jersey-spring/com/sun/jersey/spi/spring/container/servlet/package-summary.html

If you implemented the org.springframework.validation.Validator interface, you should see the the validate method expected two parameters, that is of type Object and of type Errors , so you need to add another class to your validate call 如果您实现了org.springframework.validation.Validator接口,您应该看到validate方法需要两个参数,即Object类型和Errors类型,因此您需要在验证调用中添加另一个类

BeanPropertyBindingResult v = new BeanPropertyBindingResult(dto,"Errors");
validator.validate(dto, v);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM