0

我正在使用 Jax-RS 2.0 和 Jersey 2.22.1 和 Java SE 8,部署在 Tomcat 8.0.30 中。我使用适当的 JAX-RS 注释对 POJO 进行了注释,并且它按预期工作。我还用@Singleton. 该类被懒惰地实例化为单例,这是@Singleton. 但是我想在应用程序启动时急切地实例化这个类。有没有办法做到这一点?我查看了@Startup注释,但不幸的是,这是 EJB 包的一部分,我没有使用 EJB(我也不想导入 EJB jar 文件)。

我还在使用 Springframework 4.2.4,默认情况下,它会在使用@Serviceor@Component注释时急切地实例化单例,但不幸的是,我不能在 JAX-RS POJO 上使用这些注释(这就是类从SpringBeanAutowiringSupport.

我附上了java代码,但由于它按预期工作,我不确定它会添加任何有用的东西。

import javax.inject.Singleton;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.core.MediaType;
import org.xxx.profile.util.GeneralUtil;
import org.xxx.profile.util.WebServiceLogger;
import org.xxx.profile.util.datatransfer.AccountDTO;
import org.xxx.profile.web.exception.RestException;
import org.xxx.profile.web.exception.FailureResponse;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;


@Singleton
@Path( "account" )
public class AccountWebService  extends SpringBeanAutowiringSupport{


    @Autowired
    private AccountLogic accountLogic;

    @Autowired
    protected SimpleParser simplerParser;


    @GET
    @Path("get/{id: [a-zA-Z][a-zA-Z_0-9]*}")
    @Produces( MediaType.APPLICATION_JSON )
    @Consumes( MediaType.APPLICATION_JSON )
    public AccountDTO retrieveAccount(@PathParam("id") String customerId) throws RestException {
        try {
            return accountLogic.retrieveAccount(customerId);
        }
        catch(Exception e){
            String transactionID = GeneralUtil.getUniqueID();
            WebServiceLogger.severe( transactionID, "Unable to retrieve an account for the customerId: " + customerId, e, this.getClass() );
            throw new RestException( new FailureResponse( FailureResponse.Status.FAIL, "add user friendly message here", transactionID ), e );
        }
    }

}
4

1 回答 1

1

请参阅Jersey 2.22.1 用户指南

如果要使用 Jersey Spring DI 支持,则需要将 jersey-spring3 模块添加到依赖项列表中:

<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-spring3</artifactId>
    <version>2.22.1</version>
</dependency>

它似乎也适用于 Spring 4。见问题 21443088

于 2016-02-18T22:23:13.767 回答