【问题标题】:JsonParseException when trying to run spring boot尝试运行 Spring Boot 时出现 JsonParseException
【发布时间】:2021-10-08 14:14:14
【问题描述】:

使用以下命令: java -jar target/spring-boot-config-0.0.1-SNAPSHOT.jar --spring.application.json='{"server":{"ip":"192.168.145.78"}}' 我得到一个org.springframework.boot.json.JsonParseException: Cannot parse JSON 异常。我不知道为什么,我的命令似乎是正确的。任何人都可以帮我解决这个问题。我的申请很简单:

public class SpringBootConfigApplication {
    private static Logger log = LoggerFactory.getLogger(SpringBootConfigApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(SpringBootConfigApplication.class, args);
    }

    @Value("${server.ip}")
    String serverIp;

    @Bean
    public CommandLineRunner values() {
        return args -> {
            log.info(" > The Server IP is: " + serverIp);
        };
    }
}

完整的堆栈跟踪是:

        at org.springframework.boot.json.AbstractJsonParser.trimParse(AbstractJsonParser.java:48)
        at org.springframework.boot.json.AbstractJsonParser.parseMap(AbstractJsonParser.java:36)
        at org.springframework.boot.json.YamlJsonParser.parseMap(YamlJsonParser.java:46)
        at org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor.processJson(SpringApplicationJsonEnvironmentPostProcessor.java:102)
        at org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor.lambda$postProcessEnvironment$0(SpringApplicationJsonEnvironmentPostProcessor.java:97)
        at java.base/java.util.Optional.ifPresent(Optional.java:176)
        at org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor.postProcessEnvironment(SpringApplicationJsonEnvironmentPostProcessor.java:97)
        at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEnvironmentPreparedEvent(EnvironmentPostProcessorApplicationListener.java:102)
        at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEvent(EnvironmentPostProcessorApplicationListener.java:87)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
        at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:82)
        at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:63)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
        at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:117)
        at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:111)
        at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:62)
        at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:374)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:332)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332)
        at be.brzyktom.springbootconfig.SpringBootConfigApplication.main(SpringBootConfigApplication.java:16)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        

【问题讨论】:

  • 你有堆栈跟踪吗?

标签: json spring spring-boot exception


【解决方案1】:

在外部字符串中使用双引号并在定义键和值时对其进行转义

java -jar target/spring-boot-config-0.0.1-SNAPSHOT.jar --spring.application.json="{\"server\":{\"ip\":\"192.168.145.78\"}}"

【讨论】:

  • 应该是 Windows 的情况。想知道在 unix 类型的环境中是否也需要这样做
  • 可以,但是不知道为什么Spring官网使用单引号? 2.2. JSON Application Properties
  • @LHCHIN,文档提到这些命令在基于 UNIX 的 shell 上运行。双引号适用于 Windows。
猜你喜欢
  • 1970-01-01
  • 2022-01-19
  • 2019-09-16
  • 2021-05-08
  • 1970-01-01
  • 1970-01-01
  • 2016-12-20
  • 2011-12-03
  • 2018-01-21
相关资源
最近更新 更多