apache cxf 简单 REST api returns 总是 404
apache cxf simple REST api returns always 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
。我应该如何处理这些 类 或更改或添加,以便 运行 我在服务器上的服务(。war TomEE 上的网络应用程序自动部署)?
P.S。而且我不允许使用 Spring
回答我的问题:问题是 TomEE 不支持 Java11 中的某些功能。我不得不将我的代码降级到 Java8,然后它就可以工作了。
我有一个测试任务 - 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
。我应该如何处理这些 类 或更改或添加,以便 运行 我在服务器上的服务(。war TomEE 上的网络应用程序自动部署)?
P.S。而且我不允许使用 Spring
回答我的问题:问题是 TomEE 不支持 Java11 中的某些功能。我不得不将我的代码降级到 Java8,然后它就可以工作了。