简体   繁体   English

Glassfish GET JSON Web服务500错误-未找到MessageBodyWriter

[英]Glassfish GET JSON web service 500 error - MessageBodyWriter not found

I am getting 我正进入(状态

MessageBodyWriter not found for media type=application/json, type=class java.util.HashMap$Values, genericType=java.util.Collection 找不到针对媒体type = application / json,type = class java.util.HashMap $ Values,genericType = java.util.Collection的MessageBodyWriter

from a web service with Glassfish 4.1. 使用Glassfish 4.1从Web服务中获取。 Everything I see says to have the jackson or other library in the classpath so that the types can be converted. 我看到的所有内容都说在类路径中有jackson或其他库,以便可以转换类型。 With Glassfish, I have: 使用Glassfish,我有:

~/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

Entity Classes: 实体类别:

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;
    }
}

Service class: 服务等级:

@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();
    }
}

Why am I still getting this error? 为什么我仍然收到此错误?

Yes, Glassfish has Jackson, but it also has MOXy , which is the default provider. 是的,Glassfish有Jackson,但也有MOXy ,这是默认提供程序。 MOXy is weird with some types. MOXy对于某些类型很奇怪。 If you want to disable MOXy so you can use Jackson, you need to set the following property to true 如果要禁用MOXy以便可以使用Jackson,则需要将以下属性设置为true

ServerProperties.MOXY_JSON_FEATURE_DISABLE

I haven't used Glassfish in a while, so I am not sure if you will still need to register Jackson, but I'm pretty sure you don't. 我已经有一段时间没有使用Glassfish了,所以我不确定您是否仍然需要注册Jackson,但是我敢肯定您不需要。 It should be automatically registered. 它应该被自动注册。 MOXy basically blocks it from being registered. MOXy基本上阻止了它的注册。

OK, after some digging, I came up with the following to fix it. 好的,经过一番挖掘,我提出了以下解决方案。 @peeskillet was partially right, and this fix is Glassfish specific. @peeskillet部分正确,此修复程序特定于Glassfish。

Here is the blog with more context 这是上下文更多的博客

Here is what fixed it. 这是修复它的原因。 I needed to implement the following 2 classes: 我需要实现以下2类:

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 );
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到针对媒体类型= application / json的杰克逊网络服务错误MessageBodyWriter - jackson web service error MessageBodyWriter not found for media type=application/json 找不到媒体类型= Application / json,glassfish的MessageBodyWriter - MessageBodyWriter not found for media type=Application/json, glassfish 尝试通过JAX-RS Web服务发送JSON对象,获取“找不到媒体类型= application / json的MessageBodyWriter” - Obtaining “MessageBodyWriter not found for media type=application/json” trying to send JSON object through JAX-RS web service 找不到针对媒体类型= application / json的错误MessageBodyWriter - Getting Error MessageBodyWriter not found for media type=application/json 泽西(Jersey)REST错误,找不到媒体类型= application / json的MessageBodyWriter - Jersey REST error, MessageBodyWriter not found for media type=application/json jackson-从GET Api返回JSON POJO时找不到MessageBodyWriter - jackson - MessageBodyWriter Not Found when returning a JSON POJO from a GET Api 如何在Glassfish的Web服务应用程序中解决“ HTTP状态500-内部服务器错误”? - How can i resolve the “HTTP Status 500 - Internal Server Error” in my web service app on glassfish? 从Glassfish Jersey Rest服务器获取响应Json对象时获取HTTP错误:500 - Get HTTP ERROR: 500 on get response Json Object from glassfish Jersey Rest server 尝试使用JavaEE JAX-B,Jasper和Glassfish 4.1.1获得JSON响应,但出现错误500 - Trying to get JSON response with JavaEE JAX-B, Jasper and Glassfish 4.1.1, but I get error 500 HTTP状态500 - 未找到提供程序org.glassfish.json.JsonProviderImpl - HTTP Status 500 - Provider org.glassfish.json.JsonProviderImpl not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM