0

Using the following command: java -jar target/spring-boot-config-0.0.1-SNAPSHOT.jar --spring.application.json='{"server":{"ip":"192.168.145.78"}}' I get a org.springframework.boot.json.JsonParseException: Cannot parse JSON exception. I have no idea why, my command seems to be correct. Could anybody help me with this. My application is simple:

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);
        };
    }
}

The full stacktrace is:

        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)
        
1
  • Do you have the stack trace? Commented Aug 3, 2021 at 11:33

1 Answer 1

4

Use double quotations in outer string and escape them when defining keys and values

java -jar target/spring-boot-config-0.0.1-SNAPSHOT.jar --spring.application.json="{\"server\":{\"ip\":\"192.168.145.78\"}}"
Sign up to request clarification or add additional context in comments.

3 Comments

Should be the case with Windows. Wonder whether same is required with a unix type environment
It works, but I wonder know why Spring official website uses single quotations? 2.2. JSON Application Properties
@LHCHIN, the doc mentions that those commands are run on a UNIX-based shell. Double quotes are for Windows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.