英文:
apache cxf simple REST api returns always 404
问题
以下是您要求的翻译内容:
我有一个测试任务 - 在Apache CXF中创建一个小的REST API,该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<>();
        //我添加了这行
        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也是空的。为了使我的服务在服务器上运行(.war web应用程序会自动在TomEE上部署),我应该对这些类做些什么更改或添加?
附注:我不允许使用Spring。
英文:
I have a test task - small REST api in apache CXF, which should take stock exchange rates from remote resources. I run this as a maven project (.war) and deploy through TomEE. I'm a newbie in this theme, that's why I can't figure out, why am I always receiving a 404 error.
that's my service:
@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);
    }
}
I hide the model, because the problem is surely with the deployment. Then I have several pre-created classes, like:
@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);
    }
}
My web.xml is empty (means no servlet or so tags are specified) as well as beans.xml. What should I do with those classes or change or add, in order to run my service on the server (.war web-apps on TomEE deploy automatically)?
P.S. and I'm not allowed to use Spring
答案1
得分: 0
回答我的问题:问题是TomEE不支持Java 11的某些功能。我不得不将我的代码降级到Java 8,然后它就正常工作了。
英文:
To answer my question: the problem was that TomEE didn't support some features from Java 11. I had to downgrade my code to Java 8 and it worked after.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论