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"

The decline of the big Application Server

Following on the Spring Boot tutorial, it seems that we have moved away from the large Application Servers that I used to deploy a WAR to. It has Tomcat, Jetty and Undertow. When I worked in the JEE world we used WebLogic, Jetty was just becoming a thing, and I honestly didn’t pay it much attention. But I just read this really interesting article about how things have moved on.

https://blog.fabric8.io/the-decline-of-java-application-servers-when-using-docker-containers-edbe032e1f30

It makes sense, especially in this whole microservices landscape. I’m learning a bit about how to configure aspects of the application server now. I must admit that I didn’t really understand this too much at the time either. I just coded, other people set this up for me to deploy to, but I always felt a bit concerned that I didn’t understand how to properly configure the application servers. In this whole new DevOps world, I guess I’ll have to look at this too.

Updating greyed-out Maven dependencies in-between changes

https://crunchify.com/mavenmvn-clean-install-update-project-and-project-clean-options-in-eclipse-ide-to-fix-any-dependency-issue/

Saved my day. As I am following along with a tutorial, I’m adding to and changing an existing project. Things are bound to break during the changes, but my Maven test dependencies suddenly started to be greyed out, resulting in import errors in the auto-generated test class.

To fix this I just used Task 2,

  • Right click on Project
  • Click on Maven
  • Click on Update Project...

Fixed 😀

 

@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.

 

Gradle in eclipse pain

I tried to follow along with the videos in the O’Reilly Spring Boot introduction, but it was painful. I couldn’t get eclipse to notice the Path changes to use the JDK not the JRE, so couldn’t get things to build. The names have changed from buildRepackage to buildJar. I couldn’t get it to refresh to see the jar being generated etc.

A lot of this is due to my unfamiliarity with eclipse and gradle. It’s easy to find your way of doing things and stick with it, and not really become an expert in a tool even if you’ve been using it for years.

Anyway, I also followed along on the command line and managed to build and package a jar and war eventually.

It doesn’t seem to be working properly though as localhost:8080 returns an error. Oh well.

Anyway, to see the build folder, I had to deselect the Filter on the Package Explorer tab for the Gradle build folder. F5 referesh, and there it is 🙂

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 🙂