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
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.