Json Jackson 在没有 setter 的情况下反序列化对象

我有类(例如。在实际项目中,我有 JAXB 生成的类而不是用户):


public class User {

  private List<String> users;


  public List<String> getUsers() {

    return users;

  }

}

当我获得用户用户(带有数据)时,我可以使用 JacksonJson 将其序列化为 String 或 byte[]。但是当我尝试反序列化它时,User我得到错误:


Cannot construct instance of `javax.xml.bind.JAXBElement` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

因为 User 没有 setter。我怎样才能反序列化它?


大话西游666
浏览 547回答 2
2回答

眼眸繁星

您可以通过实现自定义反序列化器类来实现。试试这个:String json = null;//your request jsonObjectMapper objectMapper = new ObjectMapper();SimpleModule module = new SimpleModule();module.addDeserializer(User.class,new UserDeserialzer());User user = objectMapper.registerModule(module).readValue(json,User.class);解串器类:public class UserDeserialzer extends JsonDeserializer<User>{&nbsp; &nbsp; @Override&nbsp; &nbsp; public User deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {&nbsp; &nbsp; &nbsp; &nbsp; JsonNode value = new ObjectMapper().readTree(p.getText());&nbsp; &nbsp; &nbsp; &nbsp; User user = new User();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Field field = user.getClass().getDeclaredField("users");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field.setAccessible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field.set(user,value.get("users"));&nbsp; &nbsp; &nbsp; &nbsp; }catch (Exception ex){&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return user;&nbsp; &nbsp; }}对于 Spring-boot 应用程序:添加此 bean 以在全局范围内反序列化。@Bean&nbsp; &nbsp; public Jackson2ObjectMapperBuilder configureObjectMapper() {&nbsp; &nbsp; &nbsp; &nbsp; Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper objectMapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; SimpleModule module = new SimpleModule();&nbsp; &nbsp; &nbsp; &nbsp; module.addDeserializer(User.class,new UserDeserialzer());&nbsp; &nbsp; &nbsp; &nbsp; objectMapper.registerModule(module);&nbsp; &nbsp; &nbsp; &nbsp; builder.configure(objectMapper);&nbsp; &nbsp; &nbsp; &nbsp; return builder;&nbsp; &nbsp; }

慕的地6264312

如果您更喜欢在创建 JAXB 对象时使用 Apache CXF,您可以使用 xjc 插件在生成对象期间自动创建 setter-getter 方法。这种方式对你来说可能非常实用。我遇到了同样的问题,通过这种方式,我在不更改代码的情况下解决了问题。您可以将以下部分添加到您的 pom 文件中:<build>&nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.cxf</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>cxf-codegen-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${version.apache.cxf}</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>generate-sources</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <phase>generate-sources</phase>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <sourceRoot>${basedir}/src/main/java</sourceRoot>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <wsdlOptions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <wsdlOption>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <wsdl>${basedir}/src/main/resources/wsdl/sample.wsdl</wsdl>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <wsdlLocation>classpath:wsdl/sample.wsdl</wsdlLocation>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <extraargs>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <extraarg>-xjc-Xbg</extraarg>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <extraarg>-xjc-Xcollection-setter-injector</extraarg>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </extraargs>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </wsdlOption>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </wsdlOptions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>wsdl2java</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <dependencies>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.cxf.xjcplugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>cxf-xjc-boolean</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.0.3</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.andromda.thirdparty.jaxb2_commons</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>collection-setter-injector</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>1.0</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dependencies>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; </plugins></build>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java