ObjectMapper的writeValueAsString方法返回空字符串的原因是什么?

huangapple go评论69阅读模式
英文:

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 = &quot;from&quot;) String from, 
@PathVariable(name = &quot;to&quot;) String to) throws IOException {
String url = &quot;example.com&quot;;
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&lt;String&gt; entity = new HttpEntity&lt;String&gt;(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(&quot;com.fasterxml.jackson.core:jackson-annotations:2.14.2&quot;)
implementation(&quot;com.fasterxml.jackson.core:jackson-core:2.14.2&quot;)
implementation(&quot;com.fasterxml.jackson.core:jackson-databind:2.14.2&quot;)

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 = &quot;from&quot;)String from, 
  @PathVariable(name = &quot;to&quot;)String to) throws IOException {
    String url = &quot;example.com&quot;;
    
    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&lt;String&gt; entity = new HttpEntity&lt;String&gt;(objectForRequestToReporter, httpHeaders);
    return restTemplate.postForObject(url , entity, String.class);
}

huangapple
  • 本文由 发表于 2023年5月17日 21:39:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定