Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Value("${local.server.port}") not working in Spring boot 1.5

I am upgrading Spring Boot from 1.3 to 1.5. For upgrading to 1.5 I have replaced

@SpringApplicationConfiguration(classes = TestConfig.class) @WebIntegrationTest

with

@SpringBootTest(classes = TestConfig.class)

Also, I am using

@Value("${local.server.port}") protected int port;

to get port number defined in application.properties file. I further use this port number to build a REST URL.

But after the upgrade I am getting the error below whereas the same works fine with 1.3 Spring Boot Test.

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'local.server.port' in value "${local.server.port}" at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)

Am I missing any changes that I need to do for this to work.

like image 678
Meena Chaudhary Avatar asked Apr 18 '17 09:04

Meena Chaudhary


People also ask

Can we change the port number of server in Spring Boot?

We can change the port of the Spring Boot in the following ways: By Adding the configuration in the application properties of the Spring Boot project. By Implementing the WebServerFactoryCustomizer interface in the component class.

How can you read the port number of server in Spring Boot?

Using the ServerProperties Class. ServerProperties holds the properties of the embedded web server, such as the port, the address, and the server header.


2 Answers

You have to provide a value for webEnvironment. In your case DEFINED_PORT like this

@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.DEFINED_PORT)
public class YourTest {

  @LocalServerPort           // shorthand for @Value("${local.server.port}")
  private Integer port;
  ...
}

For details see: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications

like image 99
newur Avatar answered Oct 29 '22 17:10

newur


Adding another alternate solution which I had elsewhere.

I had configured

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

and

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class YourTest {

  @LocalServerPort           // shorthand for @Value("${local.server.port}")
  private Integer port;
  ...
}

Thinking that was it, and still getting this error even when specifying web environment etc. My ${local.server.port} seemed to be always null.

After some time, I noticed that my Spring Boot startup message contained no notion of the port it was using, so apparently it really didn't listen to any port at all - which explained why it was null in the first place. Adding actual container implementation dependency:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>

Caused this to appear on my logs:

2019-02-26 18:45:47.231  INFO 12504 --- [           main] o.s.b.web.embedded.jetty.JettyWebServer  : Jetty started on port(s) 43132 (http/1.1) with context path '/'

after which local.server.port and @LocalServerPort would also work.

like image 34
eis Avatar answered Oct 29 '22 17:10

eis