management.security.enabled=false not working

I’m, following a tutorial on Safari (Master Microservices with Spring Boot and Spring Cloud ; Learn the fundamentals of Microservices to create fault tolerant Microservices
Ranga Karanam) and in the Spring Boot appendix he uses the actuator.

Obviously things have changed. The old way doesn’t work as it’s been deprecated:

management.security.enabled=false

So I used:

management.endpoints.web.exposure.include=*

Maven dependencies for the tutorial included:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-hal-browser</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
</dependency>

The username was user and the password was generated on the console output during startup.

http://localhost:8080/browser/index.html#http://localhost:8080/actuator

Gave me what I was looking for.

http://localhost:8080/browser/index.html#http://localhost:8080/book

Gave me my book data in JSON that was hard-coded into the @GetMapping for /book in my BooksController @RestController.

Actuators

Can’t follow the Safari Spring Boot tutorial again as it is out of date, but found this lovely article:

https://www.baeldung.com/spring-boot-actuators

It describes the changes per version. Everything is under /actuator now, and we only get health and info by default.

BUT – make sure you download the spring-boot-starter-actuator, and not the spring-boot-actuator, or you’ll get nothing!

The server time zone value ‘GMT Summer Time’ is unrecognized

When trying to complete the JNDI part of the tutorial, this error came up on the Pivotal tc server. However, from Googling it seems its a problem with the mysql connector.

I put serverTimezone=UTC onto my database url in my context.xml file and it was fine. I’m not over-thinking it… moving on.

url="jdbc:mysql://localhost:3306/test_schema?serverTimezone=UTC"

@ConfigurationProperties locations deprecated

The example tells me to add:

@ConfigurationProperties(prefix=”my”, locations=”classpath:example.properties”)

However, it was removed from Spring 1.5 I believe, so that’s not available. Googling told me to add this annotation:

@PropertySource(“classpath:example.properties”)

But that doesn’t work either. The reason is because:

“In certain situations, it may not be possible or practical to tightly control property source ordering when using @ProperySource annotations. For example, if the @Configuration classes above were registered via component-scanning, the ordering is difficult to predict.”

Taken from:

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html

I think I’ll just leave this example. The video was trying to show all the different ways to get the property, using Profiles and different ways to wire up the beans. Honestly, it’s all a bit of a muddle at this stage, so I’m not going to debug old deprecated code.

 

Spring Boot Autoconfiguration

Spring Boot is very clever. By specifying @EnableAutoConfiguration on the main class of the application, it goes through your PATH and looks at what you have (what your Maven Dependencies are). It then makes sure to include everything you may need and configures them with sensible defaults so it all works.

However, you can change all of these things. For example, to change Tomcat to use a different port and context you can go to Run > Run Configurations > Spring Boot App, select your app from the left hand side > Arguments tab, and then there’s a space to add Program arguments. The example I was following was a bit old. Google resulted in https://www.baeldung.com/spring-boot-context-path, so:

--server.port=9000 --server.context-path=/test

changed to:

--server.port=9000 --server.servlet.context-path=/test

The article shows 5 ways to change the context path and shows the priority applied to each way, as you can imagine that these settings would be duplicated and would probably be different at different times. It shouldn’t, but you know it would be! It could always be used as a fallback I suppose too.

Must remember to press the Relaunch spring boot icon to pick up changes, which in STS is a red square with a green triangle over it to the bottom right.

I next moved these properties into src/main/resources/application.properties

server.port=9000
server.servlet.context-path=/test

I then added two more. ‘name’ is used in the Application.java file, and ‘type’ is used in ‘name’ in the properties file.

type=Framework
name=Spring Boot ${type}

Application.java:

@Value("${name}")
String name;

//end-point for the rest controller
@RequestMapping("/")
public String home() {
return name;
}

 

Getting first JUnit Test to Run/Pass

In the Manual Configuration chapter, Start POMs section, the out of date code does not work. I have downloaded all the recent releases, but two of the annotations have been removed and replaced.

//import org.springframework.boot.test.SpringApplicationConfiguration;
//@SpringApplicationConfiguration(Application.class)

import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(classes = Application.class)

//import org.springframework.boot.test.WebIntegrationTest;
//@WebIntegrationTest()

import org.springframework.test.context.web.WebAppConfiguration;
@WebAppConfiguration

public class ApplicationTest { ...

Right-click on ApplicationTest.java > Run as JUnit test now works 🙂