与泽西岛的宁静服务接口 [英] restful service interface with jersey

查看:164
本文介绍了与泽西岛的宁静服务接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用接口和实现类创建一个restful服务吗?和所有JAX-RS相关的导入进入界面?我正在使用jersey2.4和jetty8.1。

Can I create a restful service with interface and implementation class? and all JAX-RS related imports go into the interface? i am using jersey2.4 and jetty8.1.

这是我的界面:

MyService.java

MyService.java

package foo.bar; 

@Path("/abc")
public interface MyService {

     @GET
     @JSONP
     @Path("/method/{id}")
     public MyResponse getStuff(@PathParam("id") Integer id);

}

MyServiceImpl.java

MyServiceImpl.java

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 my web.xml:

<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),但它抱怨说这个 -
javax.servlet.ServletException:MultiException有1个例外。它们是:| 1。 java.lang.NoSuchMethodException:在foo.bar.MyService类中找不到合适的构造函数。|

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) ,它抱怨说这个 -
我收到HTTP ERROR 404;什么都不做;控制台上没有例外

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:MultiException有1个例外。它们是:| 1。 java.lang.NoSuchMethodException:在foo.bar.MyService类中找不到合适的构造函数。|

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

你能帮忙吗?我做错了什么?

Can you please help? What I am doing wrong?

推荐答案

虽然这是一年前发布的问题,但这是我经过几次试验后遇到的解决方案(我正在工作)使用jetty 9和jersey 2.13):而不是注释接口(使用 @Path(/ abc)),尝试注释实现类。我认为这很有道理,因为接口是抽象的而不应该绑定到物理路径。这样,界面可以在不同的路径中重复使用。

Although this is a question posted a year ago, 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 re-used in different paths.

这篇关于与泽西岛的宁静服务接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆