0

Java Map を Jersey からの Json 応答にシリアライズできるかどうかを理解しようとしています。

これが私のサービスです:

@Singleton
@Path("/")
public class ApiServiceResource {
    @Inject
    ISeedingUpdateService seedingUpdateService;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/map")
    public List<String> getMap() {
        return newArrayList(seedingUpdateService.toString());
    }
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/pojo")
    public TemplateMessage getTemplateMessage(@PathParam("param") String param) {
        return new TemplateMessage(param, seedingUpdateService.toString());
    }

    @XmlRootElement
    public static class TemplateMessage {
        public Map<String,String > param;
        public TemplateMessage() {
        }
        public TemplateMessage(String param1, String param2) {
            this.param = newHashMap();
            this.param.put(param1,param2);
        }
    }
}

Map をシリアル化できないため、getMap メソッドは失敗します -->

SEVERE: The registered message body writers compatible with the MIME media type are:
application/json ->
  com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$App
*/* ->

2 番目の方法は、POJO が内部の Map でシリアル化されているため、問題なく機能します。

私が欠けているものはありますか?

ところで、アプリは Guice で構成されているため、ここに Guice の構成を示します。

@Override
protected void configureServlets() {
    bind(ApiServiceResource.class);
    /* bind jackson converters for JAXB/JSON serialization */
    bind(MessageBodyReader.class).to(JacksonJsonProvider.class);
    bind(MessageBodyWriter.class).to(JacksonJsonProvider.class);
    Map<String,String> parameters = newHashMap();
    parameters.put("com.sun.jersey.config.property.packages", "com.delver.update.api");
    serve("/rest/*").with(GuiceContainer.class, parameters);
}
4

1 に答える 1

0

バインドする代わりにMessageBodyWriter(既に複数あるため)、アプリで行っていることを試すことができます。次の方法で、Jackson ライターと Guice の例外マッパーをバインドします。

bind(forName("com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider")).in(Scopes.SINGLETON);
bind(forName("com.fasterxml.jackson.jaxrs.json.JsonParseExceptionMapper")).in(Scopes.SINGLETON);
bind(forName("com.fasterxml.jackson.jaxrs.json.JsonMappingExceptionMapper")).in(Scopes.SINGLETON);
于 2012-11-20T13:08:09.243 に答える