带有Java可选的Getters的REST响应对象



我正在春季启动中编写REST服务。我想让我的DTO课程的一些字段作为可选的获取器访问。这是一个例子。

@JsonInclude(Include.NON_NULL)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "profileType", visible = true, defaultImpl = InvalidUserDTO.class)
@JsonSubTypes({ @JsonSubTypes.Type(value = ClientDTO.class, name = "CLIENT"), @JsonSubTypes.Type(value = DriverDTO.class, name = "DRIVER") })
public abstract class UserDTO {
    private String password;
    private Optional<String> email;

使用这种方法,在我的休息回复中,我得到了类似email: {"present" : true}的东西,而不是电子邮件的真实价值。

为了解决这个问题,我尝试将JDK8模块依赖项添加到classPath。

但是,这仅在尝试在使用选项的端点上调用我的服务时仅引起以下错误:

    {"message":"Internal Server Error","details":"Handler dispatch failed; nested exception is java.lang.AbstractMethodError: 
com.fasterxml.jackson.databind.ser.std.ReferenceTypeSerializer.withResolved(Lcom/fasterxml/jackson/databind/BeanProperty;Lcom/fasterxml/jackson/databind/jsontype/TypeSerializer;
Lcom/fasterxml/jackson/databind/JsonSerializer;Lcom/fasterxml/jackson/databind/util/NameTransformer;Lcom/fasterxml/jackson/annotation/JsonInclude$Include;)
Lcom/fasterxml/jackson/databind/ser/std/ReferenceTypeSerializer;"}

我缺少什么吗?为什么会发生?

这是ObjectMapper配置:

@SpringBootApplication
public class MainApplication extends SpringBootServletInitializer {
    @Autowired
    void configureObjectMapper(final ObjectMapper mapper) {
        mapper
            .registerModule(new ParameterNamesModule())
            .registerModule(new Jdk8Module());
    }    
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        servletContext.setInitParameter("webAppRootKey", "newfacesBackend.root");
        servletContext.setInitParameter("isLog4jAutoInitializationDisabled", "true");
        super.onStartup(servletContext);
    }
    public static void main(String[] args) throws Exception {
        SpringApplication.run(MainApplication.class, args);
    }
}

gradle依赖性:

buildscript {
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.4.RELEASE")
    }
}
apply plugin: 'com.github.ben-manes.versions'
apply plugin: 'maven-publish'
apply plugin: 'org.springframework.boot'

configurations {
    compile.exclude group:'ch.qos.logback'
}
dependencies {

    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.9.4")
    compile("com.fasterxml.jackson.module:jackson-module-parameter-names:2.9.4")

您应该用映射器注册JDK8模块,例如:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Jdk8Module());

或,

您可以通过将以下内容添加到您的主要春季启动应用程序类中:

  @Autowired
  void configureObjectMapper(final ObjectMapper mapper) {
   mapper.registerModule(new ParameterNamesModule())
  .registerModule(new Jdk8Module());
  }

ParameterNamesModule的依赖性是:

<dependency>
    <groupId>com.fasterxml.jackson.module</groupId>
    <artifactId>jackson-module-parameter-names</artifactId>
</dependency>

有这个问题,结果我有不相容的依赖项

您有依赖性org.springframework.boot:spring-boot-starter-actuator:1.5.4.RELEASE

buildscript {
  dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.4.RELEASE")
  }
}
...
dependencies {
  compile("org.springframework.boot:spring-boot-starter-actuator")
}

有自己的依赖性com.fasterxml.jackson.core:jackson-databind:2.8.8并且与您提供的另一个fasterxml依赖性不兼容:

compile("com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.9.4")

解决方案

只是降级Jackson-Datatype-jdk8版本:

compile("com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.8.8")

删除configureObjectMapper并尝试以下操作:

@Configuration
public class ObjectMapperConfig
    @Bean
    @Primary
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.build();
        objectMapper.registerModule(new Jdk8Module());
        return objectMapper;
    }
}

最新更新