我想了解是否有可能序列化Java地图到来自泽西岛的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);
}
}
}
,因为它不能序列化地图的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
*/* ->
,第二种方法的工作就好了POJO的序列化与地图内。
有什么,我失踪?
由APP在吉斯配置方式,所以这里是吉斯配置:
@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);
}
我不是太熟悉吉斯,但是,我没有看到你的邮件正文的作家名单JacksonJSON供应商?我知道杰克逊可以在没有额外配置的情况下序列化地图,所以很可能你的提供者根本就没有被注册。 – Perception