接口的自定义 jax-rs 实体提供程序?
我如何告诉 RESTEasy(一个 jax-rs 实现),“嘿,当您被要求提供 IFoo
时,只需继续创建一个 Foo
对象即可?”
我当前(错误的)尝试
有一个 api 访问点,我想使用它来使用 ReportPieceDAO
对象。 ReportPieceDAO
具有 List
成员。 StandardScoreReport
是一个接口,由StandardScoreReportImpl
实现。
@Path("pieces")
@PUT
@Produces("application/json")
@Consumes("application/json")
public Iterable<Long> putReportPiece( List<ReportPieceDAO> reportPieces) {
return getDataCoordinator().updateReportPieces(getAuthenticatedUser(), reportPieces);
}
在我添加 List
成员之前,此入口点运行良好。由于 StandardScoreReport
是一个抽象接口,RESTEasy 无法自动构造一个抽象接口 - 它抱怨 StandardScoreReport
没有默认构造函数。
因此,我想制作某种适配器或提供程序,在需要 StandardScoreReport
的情况下构建 StandardScoreReportImpl
:
@Provider
@Consumes("application/json")
public class StandardScoreReportProvider implements MessageBodyReader<StandardScoreReport>{
@Override
public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2,
MediaType arg3) {
return true;
}
@Override
public StandardScoreReport readFrom(Class<StandardScoreReport> arg0,
Type arg1, Annotation[] arg2, MediaType arg3,
MultivaluedMap<String, String> arg4, InputStream arg5)
throws IOException, WebApplicationException {
//I'm hoping I can just call some "default" code that
//would run if StandardScoreReportImpl were naturally
//encountered, and not have to write my own unmarshalling code.
return new StandardScoreReportImpl();
}
}
但这些代码都不会被执行。这是我的应用程序描述:
public class RESTEasyApplicationDescription extends Application
{
HashSet<Class<?>> classes = new HashSet<Class<?>>();
public RESTEasyApplicationDescription()
{
classes.add(APIRoot.class);
classes.add(ReportsRoot.class);
classes.add(StandardScoreReportProvider.class);
classes.add(StandardScoreReport.class);
classes.add(ReportPiece.class);
}
@Override
public Set<Class<?>> getClasses() {
return classes;
}
@Override
public Set<Object> getSingletons() {
return null;
}
}
How can I tell RESTEasy (a jax-rs implementation), "Hey, when you get asked for IFoo
, just go ahead and make a Foo
object?"
My current (erroneous) attempt
I have an api access point that I would like to consume ReportPieceDAO
objects. ReportPieceDAO
has as a List<StandardScoreReport>
member. StandardScoreReport
is an interface, implemented by StandardScoreReportImpl
.
@Path("pieces")
@PUT
@Produces("application/json")
@Consumes("application/json")
public Iterable<Long> putReportPiece( List<ReportPieceDAO> reportPieces) {
return getDataCoordinator().updateReportPieces(getAuthenticatedUser(), reportPieces);
}
This entry point worked well before I added the List<StandardScoreReport>
member. Because StandardScoreReport
is an abstract interface, RESTEasy can't automatically construct one - it complains that StandardScoreReport
has no default constructor.
So, I wanted to make some sort of adapter or provider that constructed a StandardScoreReportImpl
in cases where a StandardScoreReport
was required:
@Provider
@Consumes("application/json")
public class StandardScoreReportProvider implements MessageBodyReader<StandardScoreReport>{
@Override
public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2,
MediaType arg3) {
return true;
}
@Override
public StandardScoreReport readFrom(Class<StandardScoreReport> arg0,
Type arg1, Annotation[] arg2, MediaType arg3,
MultivaluedMap<String, String> arg4, InputStream arg5)
throws IOException, WebApplicationException {
//I'm hoping I can just call some "default" code that
//would run if StandardScoreReportImpl were naturally
//encountered, and not have to write my own unmarshalling code.
return new StandardScoreReportImpl();
}
}
But none of this code is ever executed. Here's my application description:
public class RESTEasyApplicationDescription extends Application
{
HashSet<Class<?>> classes = new HashSet<Class<?>>();
public RESTEasyApplicationDescription()
{
classes.add(APIRoot.class);
classes.add(ReportsRoot.class);
classes.add(StandardScoreReportProvider.class);
classes.add(StandardScoreReport.class);
classes.add(ReportPiece.class);
}
@Override
public Set<Class<?>> getClasses() {
return classes;
}
@Override
public Set<Object> getSingletons() {
return null;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您将应用程序部署到服务器时,会注册提供程序。服务器自动知道如何处理接收/传递某个对象。
您必须创建一个具有
@Provider
的类,然后如果您并指定类型。
示例:
我有一项服务,当我打电话时给我一个 PDF,它不消耗任何东西,它只给我一个 pdf 文件:
MyService.java
MyPDFProvider.java
Providers are registered when you deploying the application to the server. The server automatically know how handle receive / deliver a certain object.
You must create a class that has the
@Provider
and then if youand specifying the type.
Example:
I have a service that gives me a PDF when I call, it's no consumes nothing, it's only give to me a pdf file:
MyService.java
MyPDFProvider.java