英文:
Why ObjectMapper writeValueAsString return empty string?
问题
以下是您提供的代码的翻译:
我需要将Java `Object` 序列化为 `JSON`。
但当我使用 `writeValueAsString` 方法时,该方法返回一个空的JSON对象。
这是我的 `@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(某些数据);
objectForRequestToReporter.setEmployee(某些数据);
objectForRequestToReporter.setFrom(from);
objectForRequestToReporter.setTo(to);
String jsonObject = objectMapper.writeValueAsString(objectForRequestToReporter);
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(objectForRequestToReporter);
System.out.println(test);
System.out.println(jsonObject);
return ans;
}
`System.out.println` 显示:
{ }
{}
Gradle 依赖:
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;
}
我假设您只需要翻译代码部分。如果您有其他需求,请随时告诉我。
英文:
I need to serialize Java Object
to JSON
.
But when I use writeValueAsString
method this method returns me empty JSON Object.
Here is my @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
shows this:
{ }
{}
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")
Object class for serialize:
@Data
public class ObjectForRequestToReporter {
private String token;
private String employee;
private String from;
private String to;
private final int work_after = 0;
}
What am I doing wrong? How can I fix this?
答案1
得分: 1
I think you have a mistake in this line:
String jsonObject = objectMapper.writeValueAsString(objectMapper);
Maybe it should be:
String jsonObject = objectMapper.writeValueAsString(objectForRequestToReporter);
英文:
I think you have a mistake in this line:
String jsonObject = objectMapper.writeValueAsString(objectMapper);
Maybe it should be:
String jsonObject = objectMapper.writeValueAsString(objectForRequestToReporter);
答案2
得分: 0
你正在尝试将ObjectMapper
转换为JSON。
你想要的是:
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<>(objectForRequestToReporter, httpHeaders);
return restTemplate.postForObject(url , entity, String.class);
}
英文:
String jsonObject = objectMapper.writeValueAsString(objectMapper);
You are trying to convert the ObjectMapper
to json.
What you want:
String jsonObject = objectMapper.writeValueAsString(objectForRequestToReporter);
However your code is doing basically what the RestTemplate
already does, and thus can be greatly simplified by removing the ObjectMapper
as you don't need it.
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论