为什么 ObjectMapper writeValueAsString 返回空字符串?

Why ObjectMapper writeValueAsString return empty string?

提问人:Khaski 提问时间:5/17/2023 最后编辑:ℛɑƒæĿᴿᴹᴿKhaski 更新时间:5/17/2023 访问量:399

问:

我需要将 Java 序列化为 .ObjectJSON

但是当我使用方法时,此方法返回给我空的JSON对象。writeValueAsString

这是我的:@RestController

public String getReportsFromReporter(
            @PathVariable(name = "from") String from, 
            @PathVariable(name = "to") String to) throws IOException {
    String url = "example.com";
    ObjectMapper objectMapper = new ObjectMapper();
    ObjectForRequestToReporter objectForRequestToReporter =
                               new ObjectForRequestToReporter();
    objectForRequestToReporter.setToken(somedata);
    objectForRequestToReporter.setEmployee(somedata);
    objectForRequestToReporter.setFrom(from);
    objectForRequestToReporter.setTo(to);
    String jsonObject = objectMapper.writeValueAsString(objectMapper);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    HttpEntity<String> entity = new HttpEntity<String>(jsonObject, httpHeaders);
    String ans = restTemplate.postForObject(url , entity, String.class);
    String test = objectMapper.writerWithDefaultPrettyPrinter()
                              .writeValueAsString(objectMapper);
    System.out.println(test);
    System.out.println(jsonObject);
    return ans;
}

System.out.println显示这一点:

 { }
 {}

Gradle depedencies:

implementation("com.fasterxml.jackson.core:jackson-annotations:2.14.2")
implementation("com.fasterxml.jackson.core:jackson-core:2.14.2")
implementation("com.fasterxml.jackson.core:jackson-databind:2.14.2")

序列化的对象类:

@Data
public class ObjectForRequestToReporter {
    private String token;
    private String employee;
    private String from;
    private String to;
    private final int work_after = 0;
}

我做错了什么?我该如何解决这个问题?

java json spring-boot 杰克逊 json-反序列化

评论

2赞 M. Deinum 5/17/2023
你真的在尝试自己做所有的事情,这已经由 §restTemplate 提供。此外,您正在将其编写为字符串,而不是所需的对象。但是,与其使用 objectmapper,不如放弃它,只需将 .将负责 json 转换。objectMapperObjectForRequestToReporterHttpEntityRestTemplate
0赞 knittl 5/17/2023
您正在序列化 ,而不是您的 . 没有任何要序列化的属性(它不是数据类)。->由拼写错误引起objectMapperobjectForRequestToReporterobjectMapper

答:

1赞 juanlumn 5/17/2023 #1

我认为你在这一行中有一个错误:

String jsonObject = objectMapper.writeValueAsString(objectMapper);

也许它应该是:

String jsonObject = objectMapper.writeValueAsString(objectForRequestToReporter);
0赞 M. Deinum 5/17/2023 #2
String jsonObject = objectMapper.writeValueAsString(objectMapper);

您正在尝试将 转换为 json。ObjectMapper

你想要什么:

String jsonObject = objectMapper.writeValueAsString(objectForRequestToReporter);

但是,您的代码基本上正在执行已经执行的操作,因此可以通过删除不需要的代码来大大简化。RestTemplateObjectMapper

public String getReportsFromReporter(@PathVariable(name = "from")String from, 
  @PathVariable(name = "to")String to) throws IOException {
    String url = "example.com";
    
    ObjectForRequestToReporter objectForRequestToReporter =
            new ObjectForRequestToReporter();
    objectForRequestToReporter.setToken(somedata);
    objectForRequestToReporter.setEmployee(somedata);
    objectForRequestToReporter.setFrom(from);
    objectForRequestToReporter.setTo(to);

    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    HttpEntity<String> entity = new HttpEntity<String>(objectForRequestToReporter, httpHeaders);
    return restTemplate.postForObject(url , entity, String.class);
}