【问题标题】:Json string not formatted in browser but formatted in consoleJson 字符串未在浏览器中格式化但在控制台中格式化
【发布时间】:2022-01-14 10:44:35
【问题描述】:

我正在尝试像这样输出序列化为 json 的对象

@GetMapping("/person")
public String getPerson() throws JsonProcessingException {
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    System.out.println(mapper.writeValueAsString(person)); //just for debug
    return mapper.writeValueAsString(person);
}

我在控制台中得到了我想要的:

{
  "name" : "person",
  "age" : 29,
  "listSkills" : [ "s1", "s2", "s3" ],
  "id" : 1,
  "hashCode" : 2145420209
} 

但在浏览器中它打印在一行中,只是得到不同的字体

{ "name" : "person", "age" : 29, "listSkills" : [ "s1", "s2", "s3" ], "id" : 1, "hashCode" : 2145420209 }

【问题讨论】:

  • 您的浏览器是否输出您在“查看源代码”模式下看到的文本?还是渲染的版本?
  • 但是 writeValueAsAStringReturns String - 我之前返回了一个 Person 但后来我决定让它在浏览器中看起来很漂亮并进入这个解决方案。我在 Chrome 和 Safari 中尝试过 - 结果相同。
  • 你不应该在服务器端格式化JSON。您增加了响应大小并浪费了时间。如果需要,在前端对其进行格式化,但我相信不是。

标签: java spring jackson


【解决方案1】:

问题

  1. 您希望浏览器具有与控制台相同的字体(和颜色)!? (您的期望“基于错误的假设”/您必须在意)
  2. 您不要将@[Rest]Controller 用作intended/documented
  3. 显然ResponseBody<String>(实际上是您返回的)在空白方面的行为与预期不符。

解决方案

  1. Spring-boot-starter-web[x]

  2. application.properties:

    spring.jackson.serialization.indent_output=true
    
  3. 实施:

    • 人员:
      package com.example.webtest;
      import java.time.LocalDate;
      
      public class Person {
         private String name;
         private LocalDate date;
         // default constructor (implicit)
         // getters, setters:
         public String getName() {
           return name;
         }
         public Person setName(String name) {
           this.name = name;
           return this; // fluent api (not mandatory...)
         }
         public LocalDate getDate() {
           return date;
         }
         public Person setDate(LocalDate date) {
           this.date = date;
           return this;
         }
      }
      
    • 应用程序 + 控制器:
      package com.example.webtest;
      
      import java.time.LocalDate;
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      @SpringBootApplication
      public class WebTestApplication {
      
         public static void main(String[] args) {
           SpringApplication.run(WebTestApplication.class, args);
         }
      
         @RestController
         static class SomeController {
      
           @GetMapping("/hello")
           public Person hello() { // Person! not String ;)
             return new Person().setName("Joe").setDate(LocalDate.now());
           }
         }
      }
      
  4. 测试(spring-boot:run,..browser)http://localhost:8080/hello:

要点

  • 使用 spring-boot "jacksonObjectMapper"(自动和属性配置)。
  • 返回所需的类型,不是字符串

【讨论】:

    【解决方案2】:

    您可以为您的浏览器安装一个 json 格式化程序,例如对于 Chrome,这个很受欢迎:JSON Formatter 或者您可以为您选择的浏览器找到类似的。

    通常它们具有出色的功能,例如 sintax 突出显示、可点击的网址、切换等等。

    【讨论】:

      【解决方案3】:

      在 Spring boot 中,GET 方法默认会产生内容类型为application/json 的结果。

      如果您想在浏览器中使用格式化字符串,请将生成设置为 text/plain,也就是

      @GetMapping(value = "/person", produces = MediaType.TEXT_PLAIN_VALUE)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-10
        • 2015-09-19
        • 1970-01-01
        • 2020-11-10
        • 1970-01-01
        • 2018-04-07
        • 1970-01-01
        • 2023-03-29
        相关资源
        最近更新 更多