“apache cxf简单REST API始终返回404”

huangapple go评论70阅读模式
英文:

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(&quot;stock&quot;)
public class StockService {
	private Stock stock = new Stock();
	
    @GET
    @Path(&quot;currencies&quot;)
	@Produces(&quot;text/json&quot;)
    public String getAllCurrencies() {
    	return stock.getAllCurrenciesJson();
    }
    
    @GET
    @Path(&quot;{currency}/{date}&quot;)
    @Produces(&quot;text/plain&quot;)
    public String getRateByDate(@PathParam(&quot;currency&quot;) String currency, 
    		@PathParam(&quot;date&quot;) String date) {
    	return stock.findRateByDate(Currency.findByShortcut(currency), date);
    }
    
    @GET
    @Path(&quot;{date}&quot;)
    @Produces(&quot;text/json&quot;)
    public String getAllRates(@PathParam(&quot;date&quot;) String date) {
    	return stock.findAllRatesByDate(date);
    }
    
    @GET
    @Path(&quot;convert/{currency}/{date}&quot;)
    @Produces(&quot;text/plain&quot;)
    public String getConversionByDate(@PathParam(&quot;currency&quot;) String currency,
    		@PathParam(&quot;date&quot;) 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(&quot;service&quot;)
public class MyApplication extends Application {

    @Override
    public Set&lt;Class&lt;?&gt;&gt; getClasses() {
    	final Set&lt;Class&lt;?&gt;&gt; classes = new HashSet&lt;&gt;();
        //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(&quot;Starting service&quot;);
        serviceRegistryService.registerService();
        logger.info(&quot;Started service&quot;);
    }
}
@ApplicationScoped
public class ServiceRegistryService {
    private static final Logger logger = LoggerFactory.getLogger(ServiceRegistryService.class);
    public void registerService() {
        serviceRegistration = ServiceRegistry
                 .createRegistration(&quot;this.service.id:v1&quot;, &quot;/service/&quot;)
                 .build();
        ServiceRegistry.submitRegistration(serviceRegistration);
        logger.info(&quot;Service registered as {}&quot;, 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.

huangapple
  • 本文由 发表于 2020年10月20日 20:14:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64444933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定