带有球衣的宁静服务界面 [英] restful service interface with jersey
问题描述
我可以创建一个带有接口和实现类的 Restful 服务吗?
Can I create a restful service with interface and implementation class?
如果是,所有 JAX-RS 相关的导入都会进入接口吗?
If so, will all JAX-RS related imports go into the interface?
我正在使用 jersey2.4 和 jetty8.1.
I am using jersey2.4 and jetty8.1.
这是我的 MyService
接口:
package foo.bar;
@Path("/abc")
public interface MyService {
@GET
@JSONP
@Path("/method/{id}")
public MyResponse getStuff(@PathParam("id") Integer id);
}
以及MyServiceImpl
那个接口的实现
package foo.bar.impl;
public class MyServiceImpl implements MyService {
public MyServiceImpl() {}
@Override
public MyResponse getStuff(Integer id) {
// do stuff
return MyResponse;
}
}
这是 web.xml 文件:
Here's the web.xml file:
<servlet>
<servlet-name>Scivantage REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>foo.bar</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
我注册了这个服务提供程序包 (foo.bar
) 但它抱怨说这个
I registered this service provider package (foo.bar
) but it complains saying this
javax.servlet.ServletException: A MultiException has 1 exceptions. They are:|1. java.lang.NoSuchMethodException: Could not find a suitable constructor in foo.bar.MyService class.
当我尝试使用实现类包(foo.bar.impl
)时,它抱怨说这个
When I tried with implementation class package (foo.bar.impl
), it complains saying this
I get HTTP ERROR 404; doesn't do anything else; no exceptions on console
当我同时尝试两者时 - 它抱怨与上述相同:
When I tried both -- it complains the same as above:
javax.servlet.ServletException: A MultiException has 1 exceptions. They are:|1. java.lang.NoSuchMethodException: Could not find a suitable constructor in foo.bar.MyService class.
我做错了什么?
推荐答案
这是我经过几次试验后发现的一个解决方案(我正在使用 jetty 9 和 jersey 2.13em>): 代替注解接口(使用@Path("/abc")
),尝试注解实现类.
Here's a solution I came across after a few trials (I'm working with jetty 9 and jersey 2.13): instead of annotate the interface (with @Path("/abc")
), try to annotate the implementation class instead.
我认为这很有意义,因为界面是抽象的",不应绑定到物理路径.这样,接口就可以在不同的路径中重用.
I think this makes good sense since interface are 'abstract' and not supposed to be bound to physical paths. This way, the interface can be reused in different paths.
这篇关于带有球衣的宁静服务界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!