[英]Documenting existing JAX-RS implementation with Swagger without annotating it
[英]Jersey JAX-RS annotating interface not the implementation class results in error
我有一个用JAX-RS注释注释的接口。 在实现类本身,我只是覆盖接口的方法; 没有覆盖注释或任何东西。
我收到以下错误: 类{name}是一个接口,无法实例化。
我已经尝试制作一个带注释的接口的jar并将其放在.war \\ lib文件夹中,但错误仍然存在。
如果重要,我正在使用JBoss的嵌入式Tomcat。
这是界面:
@javax.ws.rs.Path( "/jerseytesting.HelloWorldService" )
public interface HelloWorldService {
@javax.ws.rs.POST
@javax.ws.rs.Path( "/Greet" )
@javax.ws.rs.Consumes({"application/protobuf", "application/json"})
@javax.ws.rs.Produces({"application/protobuf", "application/json"})
jerseytesting.Twirpproto.HelloResponse greet(jerseytesting.Twirpproto.HelloRequest request);
}
以下是实施:
public class Twirpy implements Twirpproto.HelloWorldService {
@Override
public HelloResponse greet(HelloRequest request) {
HelloResponse helloResponse = HelloResponse.newBuilder().setResponse("Hello, " + request.getName()).build();
return helloResponse;
}
}
从API文档:
JAX-RS注释可以用于超类或实现接口的方法和方法参数。
因此,只有方法注释可以在接口上。
具体类应该有@Path
。 只有这样,JAX-RS才会知道创建该具体类的实例。
像这样:
@javax.ws.rs.Path( "/jerseytesting.HelloWorldService" )
public class Twirpy implements Twirpproto.HelloWorldService {
@Override
public HelloResponse greet(HelloRequest request) {
HelloResponse helloResponse = HelloResponse.newBuilder().setResponse("Hello, " + request.getName()).build();
return helloResponse;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.