DEEPAK MALIK
DEEPAK MALIK

Reputation: 11

Reading json file as string spring boot

ServiceApplicationTest{
  public static final String CONNECTED_CALL="{\"Sid\":\"835494\",\"Info\":\"121345\"}
}

Strings like "CONNECTED_CALL" can be very long, this is not preety but is working fine. My goal is to move all this kind of data to file in resource folder like - connectedCall.json and read it without making any major change.

I dont want to change already written code, I am fine with adding complexity to populate this values.

One way I had is to read these json file as string, calling this function for every value but then they cant be final.

Can i do it using any annotation? like @Value in spring boot. I am using jackson.

Upvotes: 0

Views: 1058

Answers (1)

Ashish Patil
Ashish Patil

Reputation: 4624

I think you can use @PropertySource something like :

@Configuration
@PropertySource("classpath:json.properties")
public class JsonConfig {
       
       @Value("${CONNECTED_CALL}")
       String jsonVal;
}

And your json.properties file will have :

CONNECTED_CALL="{\"Sid\":\"835494\",\"Info\":\"121345\"}

This can be one of the way.

Upvotes: 2

Related Questions