【发布时间】:2020-10-20 12:14:24
【问题描述】:
我有一个测试任务 - 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 和beans.xml 都是空的(意味着没有指定servlet 左右的标签)。为了在服务器上运行我的服务(TomEE 上的 .war web-apps 会自动部署),我应该如何处理这些类或更改或添加?
附:而且我不允许使用 Spring
【问题讨论】:
-
你的 tomee 启动日志是怎么说的?端点注册了吗?你的 conf 文件夹中有 tomee.xml 吗?
-
@Kekzpanda 很抱歉没有回答,问题是 TomEE 不支持 Java 11 功能,所以我将我的代码降级到 Java 8 并且它可以工作
标签: java rest cxf apache-tomee