[Spring Boot] How to get properties dynamically from a string contained in a URL

Get the value from the property file dynamically using Environment.

version

spring boot 2.0.3.RELEASE

Property file

application.properties


sample.name=hoge
sample.age=20

Configuration class using Environment

Inject the Environment and call the getProperty method.

SampleProperty.java


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@Configuration
// @Specify the location of the property file with PropertySource
@PropertySource("classpath:/application.properties")
public class SampleProperty {
	
	//Inject Environment
	@Autowired
	private Environment env;
	
	public String get(String key) {
		//Get property value from Environment
		return env.getProperty("sample." + key);
	}

}

RestController (a class that calls the Configuration class)

Get the value from the Configuration class with the path included in the URL as the key.

SampleController.java


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@Component
@RestController
public class SampleController {

	@Autowired
	private SampleProperty prop;

	@GetMapping(path = "/user/{key}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String getUser(@PathVariable String key) {
		
		String result = prop.get(key);
		
		if (result == null) {
			result = "The value could not be obtained.";
		}
		
		return result;
	}
}

Operation check

Access http: // localhost: 8080 / user / name from your browser

spring1.png

Access http: // localhost: 8080 / user / age from your browser spring2.png

Recommended Posts

[Spring Boot] How to get properties dynamically from a string contained in a URL
How to add a classpath in Spring Boot
How to create a Spring Boot project in IntelliJ
How to store a string from ArrayList to String in Java (Personal)
How to get the current date as a string in yyyyMMdd format
How to get the setting value (property value) from the database in Spring Framework
Convert to a tag to URL string in Rails
How to get Class from Element in Java
How to bind to property file in Spring Boot
How to set environment variables in the properties file of Spring boot application
How to write a unit test for Spring Boot 2
[Spring Boot] How to create a project (for beginners)
How to use CommandLineRunner in Spring Batch of Spring Boot
[Java] How to erase a specific character from a character string
Try Spring Boot from 0 to 100.
How to change application.properties settings at boot time in Spring boot
[Java] How to convert a character string from String type to byte type
How to call and use API in Java (Spring Boot)
How to control transactions in Spring Boot without using @Transactional
How to use Lombok in Spring
How to set Spring Boot + PostgreSQL
How to get parameters in Spark
How to use ModelMapper (Spring boot)
Upgrade spring boot from 1.5 series to 2.0 series
How to make a hinadan for a Spring Boot project using SPRING INITIALIZR
Get a proxy instance of the component itself in Spring Boot
[Java] Get data from DB using singleton service in Spring (Boot)
What I did in the migration from Spring Boot 1.4 series to 2.0 series
What I did in the migration from Spring Boot 1.5 series to 2.0 series
How to get and add data from Firebase Firestore in Ruby
[Java] How to convert from String to Path type and get the path
How to get JDK 11 on your mac in a comfortable way
How to get date data in Ruby
How to display characters entered in Spring Boot on a browser and reference links [Introduction to Spring Boot / For beginners]
How to develop from VScode in a remote destination environment or a remote destination container environment
How to insert a video in Rails
How to include Spring Tool in Eclipse 4.6.3?
[IOS] How to get data from DynamoDB
Story when moving from Spring Boot 1.5 to 2.1
Changes when migrating from Spring Boot 1.5 to Spring Boot 2.0
Notation to put a variable in a string
[Java] How to get to the front of a specific string using the String class
Changes when migrating from Spring Boot 2.0 to Spring Boot 2.2
How to get the date in java
How to not start Flyway when running unit tests in Spring Boot
How to get the absolute path of a directory running in Java
How to publish a library in jCenter
How to split Spring Boot message file
How to create a route directly from the URL you want to specify + α
How to get the ID of a user authenticated with Firebase in Swift
03. I sent a request from Spring Boot to the zip code search API
Sign in to a Spring Boot web application on the Microsoft ID platform
[Rails] How to get rid of flash messages in a certain amount of time
Get the path defined in Controller class of Spring boot as a list
How to get an arbitrary digit from a number of 2 or more digits! !!
[Spring sample code included] How to create a form and how to get multiple records
How to start a subscript from an arbitrary number in Ruby iterative processing
From creating a Spring Boot project to running an application with VS Code
How to get keycloak credentials in interceptor class
[Android] How to convert a character string to resourceId
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)