我正在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")
答案 0 :(得分:0)
您应该使用以下映射器注册JDK8模块:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Jdk8Module());
OR,
您可以通过将以下内容添加到主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>
答案 1 :(得分:0)
删除configureObjectMapper
并尝试此操作:
@Configuration
public class ObjectMapperConfig
@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.build();
objectMapper.registerModule(new Jdk8Module());
return objectMapper;
}
}
答案 2 :(得分:0)
遇到这个问题,原来我的依赖项不兼容
您具有依赖性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")