使用Java可选getter的REST响应对象

问题描述 投票:0回答:3

我正在Spring Boot中编写REST服务。我想让我的DTO类的一些字段作为Optional getters访问。这是一个例子。

@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;

有了这样的方法,在我的REST响应中,我得到的东西就像email: {"present" : true}而不是电子邮件的真正价值。

为了解决这个问题,我尝试将Jdk8Module依赖项添加到类路径中。

但是,这只会在尝试在使用Optionals的端点上调用我的服务时导致以下错误:

    {"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")
java json spring jackson optional
3个回答
1
投票

有这个问题,原来我有不兼容的依赖

你有依赖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,并且与您提供的另一个fastxml依赖项不兼容:

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")

0
投票

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

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

要么,

您可以通过将以下内容添加到主Spring Boot Application Class来全局执行此操作:

  @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>

0
投票

删除configureObjectMapper并试试这个:

@Configuration
public class ObjectMapperConfig

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

}
© www.soinside.com 2019 - 2024. All rights reserved.