Tag: jackson

无法将java.util.ArrayList的实例反序列化为VALUE_STRING

我有一个使用Jersey构build的REST服务,并部署在AppEngine中。 REST服务实现了使用应用程序/ json媒体types的动词PUT。 数据绑定由Jackson执行。 动词使用以JSON表示的企业 – 部门关系 {"name":"myEnterprise", "departments":["HR","IT","SC"]} 在客户端,我使用gson将JSON表示转换为一个java对象。 然后,我将该对象传递给我的REST服务,它工作正常。 问题: 当我的JSON表示在集合中只有一个项目 {"name":"myEnterprise", "departments":["HR"]} 该服务不能反序列化该对象。 ATTENTION: /enterprise/enterprise: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token at [Source: org.mortbay.jetty.HttpParser$Input@5a9c5842; line: 1, column: 2 正如其他用户所报告的,解决scheme是添加标志ACCEPT_SINGLE_VALUE_AS_ARRAY(例如Jersey:不能将StringList的实例反序列化为string )。 不过,我并不是在控制一个ObjectMapper,因为在服务方面它是由Jackson透露的。 题: 有没有办法在服务端configurationObjectMapper来启用ACCEPT_SINGLE_VALUE_AS_ARRAY? 注释? web.xml中? 代码细节 Java对象: @XmlRootElement public class Enterprise { private String name; private List<String> […]

使用Jackson作为Jersey客户端序列化程序

当使用Jersey客户端API时,是否有可能使用Jackson作为JSON数据的序列化器/编组器而不是JAXB? 如果是的话如何configuration呢?

@JsonProperty字段的注释以及getter / setter

我已经inheritance了一些在getter / setter上具有@JsonProperty注解的代码。 目的是当对象使用Jackson库进行序列化时,这些字段具有特定的名称。 当前代码: private String fileName; @JsonProperty("FILENAME") public String getFileName() { return fileName; } @JsonProperty("FILENAME") public void setFileName(String fileName) { this.fileName = fileName; } 现在换另一个工具,我也需要用JsonProperty注释这个字段。 所以这将是我改变的代码: @JsonProperty("FILENAME") private String fileName; @JsonProperty("FILENAME") public String getFileName() { return fileName; } @JsonProperty("FILENAME") public void setFileName(String fileName) { this.fileName = fileName; } 有没有人在这个领域以及getter / setter上使用了相同的注解? 我在网上环顾四周,但什么也没看见。 我已经编译和运行代码,但我不确定这是否会导致任何问题。 […]

我应该如何在我的RESTful JAX-RS Web服务中logging未捕获的exception?

我有一个使用Jersey和Jackson在Glassfish 3.1.2下运行的RESTful Web服务: @Stateless @LocalBean @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @Path("users") public class UserRestService { private static final Logger log = …; @GET @Path("{userId:[0-9]+}") public User getUser(@PathParam("userId") Long userId) { User user; user = loadUserByIdAndThrowApplicableWebApplicationExceptionIfNotFound(userId); return user; } } 对于预期的exception,我抛出相应的WebApplicationException ,并且如果发生意外的exception,则返回HTTP 500状态。 我现在想为这些意想不到的exception添加日志logging,但是尽pipesearch,但无法find我应该如何去做这件事。 无果而终的尝试 我已经尝试过使用Thread.UncaughtExceptionHandler并且可以确认它应用在方法体内部,但是它的uncaughtException方法从来没有被调用,因为别的东西在它们到达我的处理程序之前处理了未捕获的exception。 其他想法:#1 我见过一些人使用的另一个选项是一个ExceptionMapper ,它捕获所有exception,然后过滤掉WebApplicationException: @Provider public class ExampleExceptionMapper implements ExceptionMapper<Throwable> { private static final […]

jackson真的无法将json反序列化成generics?

这是一个重复的问题,因为下面的问题要么混乱,要么根本没有答案: 反序列化-A-通用型与jackson jackson反序列化-到-运行指定的类 用jackson反序列化,generics类 jackson反序列化generics类variables 我希望这个问题能够最终find一个让问题清楚的答案。 有一个模型: public class AgentResponse<T> { private T result; public AgentResponse(T result) { this.result = result; } public T getResult() { return result; } } JSONinput: {"result":{"first-client-id":3,"test-mail-module":3,"third-client-id":3,"second-client-id":3}} 以及两种推荐的对generics进行反序列化的方法: mapper.readValue(out, new TypeReference<AgentResponse<Map<String, Integer>>>() {}); 要么 JavaType javaType = mapper.getTypeFactory().constructParametricType(AgentResponse.class, Map.class); mapper.readValue(out, javaType); jackson永远不能处理genericstypesT,它认为它是一个来自JavaType的Map,但是由于types擦除,它findObjecttypes的构造函数参数,并引发错误。 那么这是一个jackson错误,还是我做错了什么? 还有什么明确的TypeReference或JavaType规范? com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for […]

使用Jackson ObjectMapper和Java 8可选值

我试图使用jackson写一个类的值为JSON,可选的字段: public class Test { Optional<String> field = Optional.of("hello, world!"); public Optional<String> getField() { return field; } public static void main(String[] args) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); System.out.println(mapper.writeValueAsString(new Test())); } } 执行时,这个类将生成以下输出: {"field":{"present":true}} 我明白当前/不存在的领域被包括在内,并且可以在阅读JSON数据时解决它,但是我无法解决这个事实,即可选的实际内容从不写入输出。 🙁 任何解决方法在这里,除了根本不使用ObjectMapper?

用jackson反序列化成自定义对象的HashMap

我有以下class级: import org.codehaus.jackson.annotate.JsonIgnoreProperties; import org.codehaus.jackson.annotate.JsonProperty; import java.io.Serializable; import java.util.HashMap; @JsonIgnoreProperties(ignoreUnknown = true) public class Theme implements Serializable { @JsonProperty private String themeName; @JsonProperty private boolean customized; @JsonProperty private HashMap<String, String> descriptor; //…getters and setters for the above properties } 当我执行下面的代码: HashMap<String, Theme> test = new HashMap<String, Theme>(); Theme t1 = new Theme(); t1.setCustomized(false); t1.setThemeName("theme1"); test.put("theme1", […]

如何修改Java中的JsonNode?

我需要在Java中更改JSON属性的值,我可以正确地获取值,但是我无法修改JSON。 这里是下面的代码 JsonNode blablas = mapper.readTree(parser).get("blablas"); for (JsonNode jsonNode : blablas) { String elementId = jsonNode.get("element").asText(); String value = jsonNode.get("value").asText(); if (StringUtils.equalsIgnoreCase(elementId, "blabla")) { if(value != null && value.equals("YES")){ // I need to change the node to NO then save it into the JSON } } } 做这个的最好方式是什么?

jackson+生成器模式?

我希望Jackson用下面的构造函数反序列化一个类: public Clinic(String name, Address address) 反序列化第一个参数很容易。 问题是,地址被定义为: public class Address { private Address(Map<LocationType, String> components) … public static class Builder { public Builder setCity(String value); public Builder setCountry(String value); public Address create(); } } 并构造如下: new Address.Builder().setCity("foo").setCountry("bar").create(); 有没有办法让jackson的键值对自己构build地址? 另外,有没有办法让jackson使用Builder类本身?

如何在JsonNode中创build插入新节点?

我有一个新创build的JsonNode JsonNode jNode = new ObjectCodec().createObjectNode(); 有了这个节点,我该如何在其中添加键值对,以便我可以用新值构造这个新节点? 我在http://www.cowtowncoder.com/blog/archives/2011/08/entry_460.html中提到了有关使用的内容 jNode.with("newNode").put("key1","value1"); 但是查看Jackson的JsonNode(v1.8)的API并不显示任何方法。