com.sun.jersey.spi.container.ContainerResponse:Java类的消息体编写器
尝试使用DropWizard和视图时出现问题com.sun.jersey.spi.container.ContainerResponse:Java类的消息体编写器,java,jersey,dropwizard,Java,Jersey,Dropwizard,尝试使用DropWizard和视图时出现问题 com.sun.jersey.spi.container.ContainerResponse: A message body writer for Java class com.linker.views.TicketView, and Java type class com.linker.views.TicketView, and MIME media type text/html was not found. The registered
com.sun.jersey.spi.container.ContainerResponse: A message body writer for Java class com.linker.views.TicketView, and Java type class com.linker.views.TicketView, and MIME media type text/html was not found.
The registered message body writers compatible with the MIME media type are:
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.server.impl.template.ViewableMessageBodyWriter
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
这就是我调用假定返回视图的函数的方式
@GET
@Path("/list")
@Produces(MediaType.TEXT_HTML)
public TicketView listTickets() {
return new TicketView();
}
这是我的TicketView类的一个简单版本
public class TicketView extends View {
public TicketView(){
super("test.ftl");
}
}
最后,我的maven依赖性受到了影响(我想)
com.sun.jersey.contribs
泽西多部分
1.18.3
有人知道我做错了什么吗?或者至少帮助我走上正确的道路。在您的Jersey应用程序配置中,您应该注册特殊功能以启用HTML body writer。 如果您有java base config,它可能类似于:
public class RestApplication extends ResourceConfig {
public RestApplication() {
packages("lv.autentica");
// registering JSON support
register(JacksonFeature.class);
}
}
不幸的是,我不知道Html支持功能的确切名称。
您应该检查是否确实需要返回HTML作为响应。使用Jersey时,这看起来很奇怪。您需要为com.linker.views.TicketView类实现消息正文编写器类,如下所示:
@Provider
@Produces({MediaType.TEXT_HTML})
public TicketViewWriter implements MessageBodyWriter<TicketView> {
@Override
public boolean isWriteable(
final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType
) {
return (
mediaType.isCompatible(MediaType.valueOf(MediaType.TEXT_HTML))
) && TicketView.class.isAssignableFrom(type);
}
@Override
public long getSize(
final TicketView view, final Class<?> type, final Type genericType, final Annotation[] annotations,
final MediaType mediaType
) {
return -1;
}
@Override
public void writeTo(
final TicketView view, final Class<?> type, final Type genericType, final Annotation[] annotations,
final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream
) throws IOException {
try {
// your logic here
} catch (Exception e) {
//log exception
}
}
}
@Provider
@生成({MediaType.TEXT_HTML})
public TicketViewWriter实现MessageBodyWriter{
@凌驾
公共布尔值是可写的(
最终类类型,最终类型genericType,最终注释[]注释,最终MediaType MediaType
) {
返回(
mediaType.isCompatible(mediaType.valueOf(mediaType.TEXT_HTML))
)&&TicketView.class.isAssignableFrom(类型);
}
@凌驾
公共长尺寸(
最终TicketView视图、最终类类型、最终类型genericType、最终注释[]注释、,
最终媒体类型媒体类型
) {
返回-1;
}
@凌驾
公开无效令状(
最终TicketView视图、最终类类型、最终类型genericType、最终注释[]注释、,
最终MediaType MediaType、最终多值Map HttpHeader、最终OutputStream entityStream
)抛出IOException{
试一试{
//你的逻辑在这里
}捕获(例外e){
//日志异常
}
}
}
这只是一个示例,由您决定如何使用“writeTo”方法。您可能还必须实现MessageBody reader类。看一看我的问题的解决方案如下。如上所述。我忘了在我的应用程序类中添加
viewbundle
@Override
public void initialize(Bootstrap<SupportServerConfiguration> bootstrap) {
bootstrap.addBundle(new ViewBundle());
}
@覆盖
公共无效初始化(引导引导引导){
addBundle(新的ViewBundle());
}
这解决了我的问题你说呢?我正要写下我找到了解决办法:p我犯了个错误。我忘了添加bundle wierd是我用Dropwizard做了两个服务器。我第一次使用hibernate。现在我尝试使用jdbc。但是当我使用hibernate时,我从来没有做过这样的事情。可能是我忘了导入一些可以帮助我解决这个问题的东西吗?我认为Hibernate在数据库中持久化实体的方法有点不同。也许这就是Hibernate没有抛出上述异常的原因。为什么您需要一个html响应?
@Override
public void initialize(Bootstrap<SupportServerConfiguration> bootstrap) {
bootstrap.addBundle(new ViewBundle());
}