java - 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);
}
}
Myweb.xml
为空(表示未servlet
指定或指定标签)以及beans.xml
. 为了在服务器上运行我的服务(TomEE 上的 .war web-apps 自动部署),我应该如何处理这些类或更改或添加?
PS,我不允许使用 Spring
解决方案
回答我的问题:问题在于 TomEE 不支持 Java 11 的某些功能。我不得不将我的代码降级到 Java 8,然后它就可以工作了。
推荐阅读
- python - 获取表格HTML数据时,如何获取没有文本值的``````标签在Selenium Python中的列表中占据位置?
- docker - 如何在 Docker 映像中保留 Jetbrains Teamcity 服务器实例的状态,以便在没有我的本地目录卷的情况下部署到其他人
- c# - 如何在事件处理程序中调用 android 特定代码
- powershell - 如何使用 PowerShell 处理 GUI 按钮以执行带参数的函数?
- math - 如何为以下匹配问题编写最有效的代码?
- excel - 如果有任何失败,如何显示所有失败,如果没有,则显示没有失败框
- reactjs - React/React Hooks:输入时出现未知的道具类型错误,无法弄清楚如何解决
- python - 如何在给定时间记录和保存用户可编辑的 Django 模型实例
- javascript - 将 HTML 输出解析为本地文件?
- reactjs - How to fix my useEffect or firestore functions to render the profile in the table