提问人:Khaski 提问时间:5/17/2023 最后编辑:ℛɑƒæĿᴿᴹᴿKhaski 更新时间:5/17/2023 访问量:399
为什么 ObjectMapper writeValueAsString 返回空字符串?
Why ObjectMapper writeValueAsString return empty string?
问:
我需要将 Java 序列化为 .Object
JSON
但是当我使用方法时,此方法返回给我空的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;
}
我做错了什么?我该如何解决这个问题?
答:
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);
但是,您的代码基本上正在执行已经执行的操作,因此可以通过删除不需要的代码来大大简化。RestTemplate
ObjectMapper
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);
}
评论
objectMapper
ObjectForRequestToReporter
HttpEntity
RestTemplate
objectMapper
objectForRequestToReporter
objectMapper