我得到了
找不到媒体类型为MessageBodyWriter =application/json、type=class java.util.HashMap$Values、genericType=java.util.Collection
来自一个带有Glassfish 4.1的web服务。我看到的每一件事都说要在类路径中有jackson或其他库,这样就可以转换类型。用玻璃鱼,我有:
~/apps/glassfish-4.1/glassfish/domains/domain1/logs$ find ../../../../ | grep jackson
../../../../glassfish/modules/jackson-core.jar
../../../../glassfish/modules/jackson-annotations.jar
../../../../glassfish/modules/jackson-databind.jar
../../../../glassfish/modules/jackson-jaxrs-json-provider.jar
../../../../glassfish/modules/jackson-jaxrs-base.jar
../../../../glassfish/modules/jersey-media-json-jackson.jar实体类:
public class Actor extends AbstractBaseEntity{
public Actor(String id, String name) {
super(id, name);
}
}
public class AbstractBaseEntity {
String identifier;
String name;
public AbstractBaseEntity(String id, String name){
this.identifier = id;
this.name = name;
}
public String getIdentifier() {
return identifier;
}
public String getName() {
return name;
}
}服务舱:
@Path("actors")
public class MockActorService {
private static final int DEFAULT_COUNT = 5;
HashMap<String, Actor> items;
public MockActorService() throws WFlowServiceException {
this(MockActorService.DEFAULT_COUNT);
}
public MockActorService(int actors) throws WFlowServiceException {
items = new HashMap<>();
for (int i = 0; i < actors; i++) {
Actor a = new Actor("ID:" + i, "Actor Name " + i);
items.put(a.getIdentifier(), a);
}
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getAll() throws WFlowServiceException {
GenericEntity< Collection< Actor > > entity;
Collection<Actor> vals = items.values();
entity = new GenericEntity<Collection<Actor>>(vals){};
return Response.ok(entity).build();
}
}为什么我还会犯这个错误?
发布于 2016-01-20 17:23:27
是的,Glassfish有Jackson,但它也有MOXy,这是默认的提供者。有些类型的MOXy很奇怪。如果要禁用MOXy以便可以使用杰克逊,则需要将以下属性设置为true
我已经有一段时间没有使用玻璃鱼了,所以我不确定你是否还需要注册杰克逊,但我很确定你没有,它应该自动注册。MOXy基本上阻止它注册。
发布于 2016-01-21 12:30:39
好的,经过一番挖掘,我想出了以下的方法来解决这个问题。@peeskillet在一定程度上是正确的,这种修正是针对玻璃鱼的。
以下是包含更多上下文的博客
这是修复它的方法。我需要实现以下两个类:
public class JacksonFeature implements Feature {
Logger L = LoggerFactory.getLogger(JacksonFeature.class);
@Override
public boolean configure(final FeatureContext context) {
String postfix = "";
postfix = '.' + context.getConfiguration().getRuntimeType().name().toLowerCase();
context.property(CommonProperties.MOXY_JSON_FEATURE_DISABLE + postfix, true);
L.info("Set property to true disable MOXY: " + CommonProperties.MOXY_JSON_FEATURE_DISABLE + postfix);
context.register(JsonParseExceptionMapper.class);
context.register(JsonMappingExceptionMapper.class);
context.register(JacksonJsonProvider.class, MessageBodyReader.class, MessageBodyWriter.class);
return true;
}
}
@javax.ws.rs.ApplicationPath("api")
public classMyResourceConfig extends ResourceConfig {
publicMyServicesResourceConfig() {
register( new GZipEncoder() );
register( JacksonFeature.class );
addMyResources();
}
private void addMyResources() {
register( Service1.class );
register( Service2.class );
}
}https://stackoverflow.com/questions/34906112
复制相似问题