gpt4 book ai didi

java - 带有 Java 可选 getter 的 REST 响应对象

转载 作者:行者123 更新时间:2023-12-02 02:14:29 25 4
gpt4 key购买 nike

我正在 Spring Boot 中编写 REST 服务。我想让 DTO 类的一些字段作为可选 getter 进行访问。这是示例。

@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 依赖项添加到类路径中。

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

    {"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());

或者,

您可以通过将以下内容添加到主 Spring Boot 应用程序类来全局执行此操作:

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

关于java - 带有 Java 可选 getter 的 REST 响应对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49497128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com