apache cxf简单的REST api总是返回404

问题描述

我有一个测试任务-apache CXF中的小型REST api,应该从远程资源获取股票汇率。我将其作为Maven项目(.war)运行并通过TomEE进行部署。我是这个主题的新手,这就是为什么我无法弄清楚,为什么我总是会收到404错误

这就是我的服务

@Path("stock")
public class StockService {
    private Stock stock = new Stock();
    
    @GET
    @Path("currencies")
    @Produces("text/json")
    public String getAllCurrencies() {
        return stock.getAllCurrenciesJson();
    }
    
    @GET
    @Path("{currency}/{date}")
    @Produces("text/plain")
    public String getRateByDate(@PathParam("currency") String currency,@PathParam("date") String date) {
        return stock.findrateByDate(Currency.findByShortcut(currency),date);
    }
    
    @GET
    @Path("{date}")
    @Produces("text/json")
    public String getAllRates(@PathParam("date") String date) {
        return stock.findAllRatesByDate(date);
    }
    
    @GET
    @Path("convert/{currency}/{date}")
    @Produces("text/plain")
    public String getConversionByDate(@PathParam("currency") String currency,@PathParam("date") String date,Double amount) {
        return stock.convert(Currency.findByShortcut(currency),amount,date);
    }
}

我隐藏了模型,因为问题肯定出在部署上。然后,我有几个预先创建的类,例如:

@ApplicationPath("service")
public class MyApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        final Set<Class<?>> classes = new HashSet<>();
        //I add this line
        classes.add(StockService.class);
        return classes;
    }

}
@Singleton
@Startup
public class MyStartupBean {

    private static final Logger logger = LoggerFactory.getLogger(MyStartupBean.class);

    private final ServiceRegistryService serviceRegistryService;

    protected MyStartupBean() {
        this.serviceRegistryService = null;
    }

    @Inject
    public MyStartupBean(ServiceRegistryService serviceRegistryService) {
        this.serviceRegistryService = serviceRegistryService;
    }

    @postconstruct
    public void init() {
        logger.info("Starting service");
        serviceRegistryService.registerService();
        logger.info("Started service");
    }
}
@ApplicationScoped
public class ServiceRegistryService {
    private static final Logger logger = LoggerFactory.getLogger(ServiceRegistryService.class);
    public void registerService() {
        serviceRegistration = ServiceRegistry
                 .createRegistration("this.service.id:v1","/service/")
                 .build();
        ServiceRegistry.submitRegistration(serviceRegistration);
        logger.info("Service registered as {}",serviceRegistration);
    }
}

我的web.xml是空的(没有指定servlet或类似的标签)以及beans.xml。为了在服务器上运行我的服务(TomEE上的.war网络应用会自动部署),我应该对那些类进行更改或更改或添加

P.S。而且我不允许使用Spring

解决方法

回答我的问题:问题是 TomEE 不支持 Java 11 的某些功能。我不得不将我的代码降级到 Java 8,然后它才能工作。