REST Jersey Service as a Spring Component

I have a leisure service in my application and I would like it to use spring beans. Spring I am using version 2.5. Part of 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:

<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 added:

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

and:

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

The interesting part of the class of service:

@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();
    }

The GET method works as expected (basically it works :)), but the POST method throws a NullPointerException on the line: if (validator.validate (dto) == 200).

I do not know why the @Autowired annotation does not work here, ale I tried with @Resource - the same effect.

Any advice could help.

Thanks in advance, Marek.

+5
source share
2 answers

org.springframework.validation.Validator, , , Object Errors,

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

All Articles