我在 Spring Boot 中使用 REST API,但收到此错误 -
java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
经过严格的调试,我发现这是因为我的域类没有在 API 响应中获取的 key “developer.email”。
我在域类中有 @JsonIgnoreProperties(ignoreUnknown = true)
注释。
Java 不允许您使用“developer.email”创建变量名
我该如何让它发挥作用? spring框架有针对这种情况的解决方法吗? 有没有办法手动配置这个实体而不是 spring 自动配置。
这是我正在尝试解析的 JSON
{"developer.email" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f6929380d89795959983988285dd929380939a9986938497868685b692999b979f98d895999b" rel="noreferrer noopener nofollow">[email protected]</a>"}
这是我当前的域类,我在这里使用lombok。
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class OauthTokenResponse {
private String refresh_token_expires_in;
private String refresh_token_status;
private String api_product_list;
private String app_enduser;
@JsonIgnore
private List<String> api_product_list_json;
private String organization_name;
@JsonIgnore
@JsonProperty("developer.email")
private String developerEmail;
private String token_type;
private String issued_at;
private String client_id;
private String access_token;
private String refresh_token;
private String application_name;
private String scope;
private String refresh_token_issued_at;
private String expires_in;
private String refresh_count;
private String status;
}
最佳答案
您是否尝试使用“@JsonProperty”注释该字段?
@JsonProperty("developer.email")
String developerEmail
小测试来证明我的观点:
public class Evil {
@JsonProperty("evil.property")
public int evil;
}
@RunWith(MockitoJUnitRunner.class)
public class MapperTest {
@Test
public void mapperTest() throws JsonParseException, JsonMappingException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final String test = "{ \"evil.property\" : 2 }";
final Evil evil = mapper.readValue(test, Evil.class);
assertThat(evil.evil, is(2));
}
}
关于java - 如果输出 json 具有带点(.) 字符的键,如何在 Spring 中为 Rest api 创建域类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46196636/