@Value in Springboot returns null
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have the application.properties, which is located in the resources
apllication.properties
hsm.provider=software
hsm.name=TestHsm
hsm.port=3001
hsm.ip=127.0.0.1
hsm.timeout=10000
and the Controller
@RestController
@RequestMapping("/hsm")
public class Controller {
@Value("${hsm.ip}")
private String ip;
@Value("${hsm.port}")
private String port;
@Value("${hsm.name}")
private String name;
@Value("${hsm.timeout}")
private String timeout;
@Value("${hsm.provider}")
private String provider;}
}
however when i run application, all variables remain NULL.
What am i missing?
EDIT
This is the projectstructure as of the src folder
src
├───main
│ ├───java
│ │ └───com
│ │ └───xyz
│ │ └───hsmservice
│ │ └───hsm
│ │ └───api
│ │ Application.java
│ │ Controller.java
│ │ HSM.java
│ │
│ └───resources
│ │ application.properties
│ │
│ └───META-INF
│ plugin.xml
│
└───test
├───java
│ LibraryTest.java
│
└───resources
EDIT 2
Here is the application class
@SpringBootApplication
public class Application {
public static void main(String args) {
SpringApplication.run(Application.class, args);
}
}
java spring rest spring-boot
|
show 1 more comment
I have the application.properties, which is located in the resources
apllication.properties
hsm.provider=software
hsm.name=TestHsm
hsm.port=3001
hsm.ip=127.0.0.1
hsm.timeout=10000
and the Controller
@RestController
@RequestMapping("/hsm")
public class Controller {
@Value("${hsm.ip}")
private String ip;
@Value("${hsm.port}")
private String port;
@Value("${hsm.name}")
private String name;
@Value("${hsm.timeout}")
private String timeout;
@Value("${hsm.provider}")
private String provider;}
}
however when i run application, all variables remain NULL.
What am i missing?
EDIT
This is the projectstructure as of the src folder
src
├───main
│ ├───java
│ │ └───com
│ │ └───xyz
│ │ └───hsmservice
│ │ └───hsm
│ │ └───api
│ │ Application.java
│ │ Controller.java
│ │ HSM.java
│ │
│ └───resources
│ │ application.properties
│ │
│ └───META-INF
│ plugin.xml
│
└───test
├───java
│ LibraryTest.java
│
└───resources
EDIT 2
Here is the application class
@SpringBootApplication
public class Application {
public static void main(String args) {
SpringApplication.run(Application.class, args);
}
}
java spring rest spring-boot
Probably your controller class is not picked up by your application scanning. Could you show us your package structure?
– Urosh T.
Nov 26 '18 at 14:02
apllication.properties
are you sure you have not a typo here? should beapplication.properties
– pleft
Nov 26 '18 at 14:04
@pleft woops, thats a typo in the question, the one i actually have in the project is correct tho.
– NicO
Nov 26 '18 at 14:14
@UroshT. ive added it
– NicO
Nov 26 '18 at 14:17
Check out SpringBoot's configuration proeprties to bind external properties to an Object that can be passed. Its cleaner than using @Value everwhere and should by proxy fix your issue. The controller may be being bean'd before the environment has fully refreshed with the properties.
– Darren Forsythe
Nov 26 '18 at 15:56
|
show 1 more comment
I have the application.properties, which is located in the resources
apllication.properties
hsm.provider=software
hsm.name=TestHsm
hsm.port=3001
hsm.ip=127.0.0.1
hsm.timeout=10000
and the Controller
@RestController
@RequestMapping("/hsm")
public class Controller {
@Value("${hsm.ip}")
private String ip;
@Value("${hsm.port}")
private String port;
@Value("${hsm.name}")
private String name;
@Value("${hsm.timeout}")
private String timeout;
@Value("${hsm.provider}")
private String provider;}
}
however when i run application, all variables remain NULL.
What am i missing?
EDIT
This is the projectstructure as of the src folder
src
├───main
│ ├───java
│ │ └───com
│ │ └───xyz
│ │ └───hsmservice
│ │ └───hsm
│ │ └───api
│ │ Application.java
│ │ Controller.java
│ │ HSM.java
│ │
│ └───resources
│ │ application.properties
│ │
│ └───META-INF
│ plugin.xml
│
└───test
├───java
│ LibraryTest.java
│
└───resources
EDIT 2
Here is the application class
@SpringBootApplication
public class Application {
public static void main(String args) {
SpringApplication.run(Application.class, args);
}
}
java spring rest spring-boot
I have the application.properties, which is located in the resources
apllication.properties
hsm.provider=software
hsm.name=TestHsm
hsm.port=3001
hsm.ip=127.0.0.1
hsm.timeout=10000
and the Controller
@RestController
@RequestMapping("/hsm")
public class Controller {
@Value("${hsm.ip}")
private String ip;
@Value("${hsm.port}")
private String port;
@Value("${hsm.name}")
private String name;
@Value("${hsm.timeout}")
private String timeout;
@Value("${hsm.provider}")
private String provider;}
}
however when i run application, all variables remain NULL.
What am i missing?
EDIT
This is the projectstructure as of the src folder
src
├───main
│ ├───java
│ │ └───com
│ │ └───xyz
│ │ └───hsmservice
│ │ └───hsm
│ │ └───api
│ │ Application.java
│ │ Controller.java
│ │ HSM.java
│ │
│ └───resources
│ │ application.properties
│ │
│ └───META-INF
│ plugin.xml
│
└───test
├───java
│ LibraryTest.java
│
└───resources
EDIT 2
Here is the application class
@SpringBootApplication
public class Application {
public static void main(String args) {
SpringApplication.run(Application.class, args);
}
}
java spring rest spring-boot
java spring rest spring-boot
edited Nov 26 '18 at 15:57
J-Alex
1
1
asked Nov 26 '18 at 13:54
NicONicO
287
287
Probably your controller class is not picked up by your application scanning. Could you show us your package structure?
– Urosh T.
Nov 26 '18 at 14:02
apllication.properties
are you sure you have not a typo here? should beapplication.properties
– pleft
Nov 26 '18 at 14:04
@pleft woops, thats a typo in the question, the one i actually have in the project is correct tho.
– NicO
Nov 26 '18 at 14:14
@UroshT. ive added it
– NicO
Nov 26 '18 at 14:17
Check out SpringBoot's configuration proeprties to bind external properties to an Object that can be passed. Its cleaner than using @Value everwhere and should by proxy fix your issue. The controller may be being bean'd before the environment has fully refreshed with the properties.
– Darren Forsythe
Nov 26 '18 at 15:56
|
show 1 more comment
Probably your controller class is not picked up by your application scanning. Could you show us your package structure?
– Urosh T.
Nov 26 '18 at 14:02
apllication.properties
are you sure you have not a typo here? should beapplication.properties
– pleft
Nov 26 '18 at 14:04
@pleft woops, thats a typo in the question, the one i actually have in the project is correct tho.
– NicO
Nov 26 '18 at 14:14
@UroshT. ive added it
– NicO
Nov 26 '18 at 14:17
Check out SpringBoot's configuration proeprties to bind external properties to an Object that can be passed. Its cleaner than using @Value everwhere and should by proxy fix your issue. The controller may be being bean'd before the environment has fully refreshed with the properties.
– Darren Forsythe
Nov 26 '18 at 15:56
Probably your controller class is not picked up by your application scanning. Could you show us your package structure?
– Urosh T.
Nov 26 '18 at 14:02
Probably your controller class is not picked up by your application scanning. Could you show us your package structure?
– Urosh T.
Nov 26 '18 at 14:02
apllication.properties
are you sure you have not a typo here? should be application.properties
– pleft
Nov 26 '18 at 14:04
apllication.properties
are you sure you have not a typo here? should be application.properties
– pleft
Nov 26 '18 at 14:04
@pleft woops, thats a typo in the question, the one i actually have in the project is correct tho.
– NicO
Nov 26 '18 at 14:14
@pleft woops, thats a typo in the question, the one i actually have in the project is correct tho.
– NicO
Nov 26 '18 at 14:14
@UroshT. ive added it
– NicO
Nov 26 '18 at 14:17
@UroshT. ive added it
– NicO
Nov 26 '18 at 14:17
Check out SpringBoot's configuration proeprties to bind external properties to an Object that can be passed. Its cleaner than using @Value everwhere and should by proxy fix your issue. The controller may be being bean'd before the environment has fully refreshed with the properties.
– Darren Forsythe
Nov 26 '18 at 15:56
Check out SpringBoot's configuration proeprties to bind external properties to an Object that can be passed. Its cleaner than using @Value everwhere and should by proxy fix your issue. The controller may be being bean'd before the environment has fully refreshed with the properties.
– Darren Forsythe
Nov 26 '18 at 15:56
|
show 1 more comment
5 Answers
5
active
oldest
votes
I've added your code into a Spring boot project and it is in a working condition.
Checkout this 53482633 repository and follow the instructions to get it up and running.
Also compare your code against this application to figure out what was going wrong at your end.
In case if you still face any issues, please post it here.
add a comment |
I had the same issue before and @Value was not working for controller but working for component classes so i had used below solution.
You can @Autowire Environment environment
and then environment.getProperty("hsm.provider")
.
Note: This is just a workaround solution.
This does not work either unfortunatly, theenvironment.getProperty("hsm.provider")
throws me in a InvocationTargetException.
– NicO
Nov 26 '18 at 14:35
Could you add to your pom : <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> ...
– TinyOS
Nov 26 '18 at 15:08
add a comment |
Judging by your package structure, those properties should definitely be loaded. Only possible option is that you have instantiated your Controller
class as new Controller()
instead of letting spring injecting the class (using @Autowired
or constructor injection).
Im actually not sure how i've instantiated it, the only other significant class is application, which starts the whole thing. Ive added the class in the question.
– NicO
Nov 26 '18 at 14:27
Just make sure you are not doingController controller = new Controller();
. See more info on how to do it right: docs.spring.io/spring-boot/docs/current/reference/html/…
– Urosh T.
Nov 26 '18 at 14:30
I didnt. You can see the other class in the new edit, in the question.
– NicO
Nov 26 '18 at 14:37
Then where do you use thisController
class? How do you know these values are null? Have you debugged it? There isn't any endpoints in your controller either...
– Urosh T.
Nov 26 '18 at 14:41
Yea, ive debugged it, i did set a breakpoint in a small constructor under the variables. I orientated myself after this tutorial by spring.
– NicO
Nov 26 '18 at 14:43
|
show 1 more comment
Controller.java
With Lombok
package com.example.demo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hsm")
@Data
public class Controller {
@Value("${hsm.ip}")
private String ip;
@Value("${hsm.port}")
private String port;
@Value("${hsm.name}")
private String name;
@Value("${hsm.timeout}")
private String timeout;
@Value("${hsm.provider}")
private String provider;
}
Without Lombok ( generated by Intelliji - Refactor DeLombok )
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hsm")
public class Controller {
@Value("${hsm.ip}")
private String ip;
@Value("${hsm.port}")
private String port;
@Value("${hsm.name}")
private String name;
@Value("${hsm.timeout}")
private String timeout;
@Value("${hsm.provider}")
private String provider;
public Controller() {
}
public String getIp() {
return this.ip;
}
public String getPort() {
return this.port;
}
public String getName() {
return this.name;
}
public String getTimeout() {
return this.timeout;
}
public String getProvider() {
return this.provider;
}
public void setIp(String ip) {
this.ip = ip;
}
public void setPort(String port) {
this.port = port;
}
public void setName(String name) {
this.name = name;
}
public void setTimeout(String timeout) {
this.timeout = timeout;
}
public void setProvider(String provider) {
this.provider = provider;
}
public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof Controller)) return false;
final Controller other = (Controller) o;
if (!other.canEqual((Object) this)) return false;
final Object this$ip = this.getIp();
final Object other$ip = other.getIp();
if (this$ip == null ? other$ip != null : !this$ip.equals(other$ip)) return false;
final Object this$port = this.getPort();
final Object other$port = other.getPort();
if (this$port == null ? other$port != null : !this$port.equals(other$port)) return false;
final Object this$name = this.getName();
final Object other$name = other.getName();
if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
final Object this$timeout = this.getTimeout();
final Object other$timeout = other.getTimeout();
if (this$timeout == null ? other$timeout != null : !this$timeout.equals(other$timeout)) return false;
final Object this$provider = this.getProvider();
final Object other$provider = other.getProvider();
if (this$provider == null ? other$provider != null : !this$provider.equals(other$provider)) return false;
return true;
}
protected boolean canEqual(final Object other) {
return other instanceof Controller;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $ip = this.getIp();
result = result * PRIME + ($ip == null ? 43 : $ip.hashCode());
final Object $port = this.getPort();
result = result * PRIME + ($port == null ? 43 : $port.hashCode());
final Object $name = this.getName();
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
final Object $timeout = this.getTimeout();
result = result * PRIME + ($timeout == null ? 43 : $timeout.hashCode());
final Object $provider = this.getProvider();
result = result * PRIME + ($provider == null ? 43 : $provider.hashCode());
return result;
}
public String toString() {
return "Controller(ip=" + this.getIp() + ", port=" + this.getPort() + ", name=" + this.getName() + ", timeout=" + this.getTimeout() + ", provider=" + this.getProvider() + ")";
}
}
DemoApplication.java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class DemoApplication {
@Autowired
Controller controller;
public static void main(String args) {
try (ConfigurableApplicationContext ctx = SpringApplication.run(DemoApplication.class, args)) {
DemoApplication app = ctx.getBean(DemoApplication.class);
app.run(args);
} catch (Exception e) {
e.printStackTrace();
}
}
public void run(String... args) throws Exception {
System.out.println(controller);
}
}
Output:
Controller(ip=127.0.0.1, port=3001, name=TestHsm, timeout=1000, provider=software)
This does not work either, it appears. I get an "Error creating bean with name 'defaultValidator'" I also cant use lombok in this project.
– NicO
Nov 26 '18 at 15:10
added code for Controller.java without Lombok, try it
– VietDD
Nov 26 '18 at 15:17
add a comment |
Okay, i solved it with the top answer of this Question. I put the variables and @Values in the signature of the constructor and not as class variables.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53482633%2fvalue-in-springboot-returns-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
I've added your code into a Spring boot project and it is in a working condition.
Checkout this 53482633 repository and follow the instructions to get it up and running.
Also compare your code against this application to figure out what was going wrong at your end.
In case if you still face any issues, please post it here.
add a comment |
I've added your code into a Spring boot project and it is in a working condition.
Checkout this 53482633 repository and follow the instructions to get it up and running.
Also compare your code against this application to figure out what was going wrong at your end.
In case if you still face any issues, please post it here.
add a comment |
I've added your code into a Spring boot project and it is in a working condition.
Checkout this 53482633 repository and follow the instructions to get it up and running.
Also compare your code against this application to figure out what was going wrong at your end.
In case if you still face any issues, please post it here.
I've added your code into a Spring boot project and it is in a working condition.
Checkout this 53482633 repository and follow the instructions to get it up and running.
Also compare your code against this application to figure out what was going wrong at your end.
In case if you still face any issues, please post it here.
answered Nov 26 '18 at 16:49
snmaddulasnmaddula
382113
382113
add a comment |
add a comment |
I had the same issue before and @Value was not working for controller but working for component classes so i had used below solution.
You can @Autowire Environment environment
and then environment.getProperty("hsm.provider")
.
Note: This is just a workaround solution.
This does not work either unfortunatly, theenvironment.getProperty("hsm.provider")
throws me in a InvocationTargetException.
– NicO
Nov 26 '18 at 14:35
Could you add to your pom : <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> ...
– TinyOS
Nov 26 '18 at 15:08
add a comment |
I had the same issue before and @Value was not working for controller but working for component classes so i had used below solution.
You can @Autowire Environment environment
and then environment.getProperty("hsm.provider")
.
Note: This is just a workaround solution.
This does not work either unfortunatly, theenvironment.getProperty("hsm.provider")
throws me in a InvocationTargetException.
– NicO
Nov 26 '18 at 14:35
Could you add to your pom : <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> ...
– TinyOS
Nov 26 '18 at 15:08
add a comment |
I had the same issue before and @Value was not working for controller but working for component classes so i had used below solution.
You can @Autowire Environment environment
and then environment.getProperty("hsm.provider")
.
Note: This is just a workaround solution.
I had the same issue before and @Value was not working for controller but working for component classes so i had used below solution.
You can @Autowire Environment environment
and then environment.getProperty("hsm.provider")
.
Note: This is just a workaround solution.
answered Nov 26 '18 at 14:12
AlienAlien
5,50331128
5,50331128
This does not work either unfortunatly, theenvironment.getProperty("hsm.provider")
throws me in a InvocationTargetException.
– NicO
Nov 26 '18 at 14:35
Could you add to your pom : <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> ...
– TinyOS
Nov 26 '18 at 15:08
add a comment |
This does not work either unfortunatly, theenvironment.getProperty("hsm.provider")
throws me in a InvocationTargetException.
– NicO
Nov 26 '18 at 14:35
Could you add to your pom : <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> ...
– TinyOS
Nov 26 '18 at 15:08
This does not work either unfortunatly, the
environment.getProperty("hsm.provider")
throws me in a InvocationTargetException.– NicO
Nov 26 '18 at 14:35
This does not work either unfortunatly, the
environment.getProperty("hsm.provider")
throws me in a InvocationTargetException.– NicO
Nov 26 '18 at 14:35
Could you add to your pom : <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> ...
– TinyOS
Nov 26 '18 at 15:08
Could you add to your pom : <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> ...
– TinyOS
Nov 26 '18 at 15:08
add a comment |
Judging by your package structure, those properties should definitely be loaded. Only possible option is that you have instantiated your Controller
class as new Controller()
instead of letting spring injecting the class (using @Autowired
or constructor injection).
Im actually not sure how i've instantiated it, the only other significant class is application, which starts the whole thing. Ive added the class in the question.
– NicO
Nov 26 '18 at 14:27
Just make sure you are not doingController controller = new Controller();
. See more info on how to do it right: docs.spring.io/spring-boot/docs/current/reference/html/…
– Urosh T.
Nov 26 '18 at 14:30
I didnt. You can see the other class in the new edit, in the question.
– NicO
Nov 26 '18 at 14:37
Then where do you use thisController
class? How do you know these values are null? Have you debugged it? There isn't any endpoints in your controller either...
– Urosh T.
Nov 26 '18 at 14:41
Yea, ive debugged it, i did set a breakpoint in a small constructor under the variables. I orientated myself after this tutorial by spring.
– NicO
Nov 26 '18 at 14:43
|
show 1 more comment
Judging by your package structure, those properties should definitely be loaded. Only possible option is that you have instantiated your Controller
class as new Controller()
instead of letting spring injecting the class (using @Autowired
or constructor injection).
Im actually not sure how i've instantiated it, the only other significant class is application, which starts the whole thing. Ive added the class in the question.
– NicO
Nov 26 '18 at 14:27
Just make sure you are not doingController controller = new Controller();
. See more info on how to do it right: docs.spring.io/spring-boot/docs/current/reference/html/…
– Urosh T.
Nov 26 '18 at 14:30
I didnt. You can see the other class in the new edit, in the question.
– NicO
Nov 26 '18 at 14:37
Then where do you use thisController
class? How do you know these values are null? Have you debugged it? There isn't any endpoints in your controller either...
– Urosh T.
Nov 26 '18 at 14:41
Yea, ive debugged it, i did set a breakpoint in a small constructor under the variables. I orientated myself after this tutorial by spring.
– NicO
Nov 26 '18 at 14:43
|
show 1 more comment
Judging by your package structure, those properties should definitely be loaded. Only possible option is that you have instantiated your Controller
class as new Controller()
instead of letting spring injecting the class (using @Autowired
or constructor injection).
Judging by your package structure, those properties should definitely be loaded. Only possible option is that you have instantiated your Controller
class as new Controller()
instead of letting spring injecting the class (using @Autowired
or constructor injection).
answered Nov 26 '18 at 14:20
Urosh T.Urosh T.
77811420
77811420
Im actually not sure how i've instantiated it, the only other significant class is application, which starts the whole thing. Ive added the class in the question.
– NicO
Nov 26 '18 at 14:27
Just make sure you are not doingController controller = new Controller();
. See more info on how to do it right: docs.spring.io/spring-boot/docs/current/reference/html/…
– Urosh T.
Nov 26 '18 at 14:30
I didnt. You can see the other class in the new edit, in the question.
– NicO
Nov 26 '18 at 14:37
Then where do you use thisController
class? How do you know these values are null? Have you debugged it? There isn't any endpoints in your controller either...
– Urosh T.
Nov 26 '18 at 14:41
Yea, ive debugged it, i did set a breakpoint in a small constructor under the variables. I orientated myself after this tutorial by spring.
– NicO
Nov 26 '18 at 14:43
|
show 1 more comment
Im actually not sure how i've instantiated it, the only other significant class is application, which starts the whole thing. Ive added the class in the question.
– NicO
Nov 26 '18 at 14:27
Just make sure you are not doingController controller = new Controller();
. See more info on how to do it right: docs.spring.io/spring-boot/docs/current/reference/html/…
– Urosh T.
Nov 26 '18 at 14:30
I didnt. You can see the other class in the new edit, in the question.
– NicO
Nov 26 '18 at 14:37
Then where do you use thisController
class? How do you know these values are null? Have you debugged it? There isn't any endpoints in your controller either...
– Urosh T.
Nov 26 '18 at 14:41
Yea, ive debugged it, i did set a breakpoint in a small constructor under the variables. I orientated myself after this tutorial by spring.
– NicO
Nov 26 '18 at 14:43
Im actually not sure how i've instantiated it, the only other significant class is application, which starts the whole thing. Ive added the class in the question.
– NicO
Nov 26 '18 at 14:27
Im actually not sure how i've instantiated it, the only other significant class is application, which starts the whole thing. Ive added the class in the question.
– NicO
Nov 26 '18 at 14:27
Just make sure you are not doing
Controller controller = new Controller();
. See more info on how to do it right: docs.spring.io/spring-boot/docs/current/reference/html/…– Urosh T.
Nov 26 '18 at 14:30
Just make sure you are not doing
Controller controller = new Controller();
. See more info on how to do it right: docs.spring.io/spring-boot/docs/current/reference/html/…– Urosh T.
Nov 26 '18 at 14:30
I didnt. You can see the other class in the new edit, in the question.
– NicO
Nov 26 '18 at 14:37
I didnt. You can see the other class in the new edit, in the question.
– NicO
Nov 26 '18 at 14:37
Then where do you use this
Controller
class? How do you know these values are null? Have you debugged it? There isn't any endpoints in your controller either...– Urosh T.
Nov 26 '18 at 14:41
Then where do you use this
Controller
class? How do you know these values are null? Have you debugged it? There isn't any endpoints in your controller either...– Urosh T.
Nov 26 '18 at 14:41
Yea, ive debugged it, i did set a breakpoint in a small constructor under the variables. I orientated myself after this tutorial by spring.
– NicO
Nov 26 '18 at 14:43
Yea, ive debugged it, i did set a breakpoint in a small constructor under the variables. I orientated myself after this tutorial by spring.
– NicO
Nov 26 '18 at 14:43
|
show 1 more comment
Controller.java
With Lombok
package com.example.demo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hsm")
@Data
public class Controller {
@Value("${hsm.ip}")
private String ip;
@Value("${hsm.port}")
private String port;
@Value("${hsm.name}")
private String name;
@Value("${hsm.timeout}")
private String timeout;
@Value("${hsm.provider}")
private String provider;
}
Without Lombok ( generated by Intelliji - Refactor DeLombok )
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hsm")
public class Controller {
@Value("${hsm.ip}")
private String ip;
@Value("${hsm.port}")
private String port;
@Value("${hsm.name}")
private String name;
@Value("${hsm.timeout}")
private String timeout;
@Value("${hsm.provider}")
private String provider;
public Controller() {
}
public String getIp() {
return this.ip;
}
public String getPort() {
return this.port;
}
public String getName() {
return this.name;
}
public String getTimeout() {
return this.timeout;
}
public String getProvider() {
return this.provider;
}
public void setIp(String ip) {
this.ip = ip;
}
public void setPort(String port) {
this.port = port;
}
public void setName(String name) {
this.name = name;
}
public void setTimeout(String timeout) {
this.timeout = timeout;
}
public void setProvider(String provider) {
this.provider = provider;
}
public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof Controller)) return false;
final Controller other = (Controller) o;
if (!other.canEqual((Object) this)) return false;
final Object this$ip = this.getIp();
final Object other$ip = other.getIp();
if (this$ip == null ? other$ip != null : !this$ip.equals(other$ip)) return false;
final Object this$port = this.getPort();
final Object other$port = other.getPort();
if (this$port == null ? other$port != null : !this$port.equals(other$port)) return false;
final Object this$name = this.getName();
final Object other$name = other.getName();
if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
final Object this$timeout = this.getTimeout();
final Object other$timeout = other.getTimeout();
if (this$timeout == null ? other$timeout != null : !this$timeout.equals(other$timeout)) return false;
final Object this$provider = this.getProvider();
final Object other$provider = other.getProvider();
if (this$provider == null ? other$provider != null : !this$provider.equals(other$provider)) return false;
return true;
}
protected boolean canEqual(final Object other) {
return other instanceof Controller;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $ip = this.getIp();
result = result * PRIME + ($ip == null ? 43 : $ip.hashCode());
final Object $port = this.getPort();
result = result * PRIME + ($port == null ? 43 : $port.hashCode());
final Object $name = this.getName();
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
final Object $timeout = this.getTimeout();
result = result * PRIME + ($timeout == null ? 43 : $timeout.hashCode());
final Object $provider = this.getProvider();
result = result * PRIME + ($provider == null ? 43 : $provider.hashCode());
return result;
}
public String toString() {
return "Controller(ip=" + this.getIp() + ", port=" + this.getPort() + ", name=" + this.getName() + ", timeout=" + this.getTimeout() + ", provider=" + this.getProvider() + ")";
}
}
DemoApplication.java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class DemoApplication {
@Autowired
Controller controller;
public static void main(String args) {
try (ConfigurableApplicationContext ctx = SpringApplication.run(DemoApplication.class, args)) {
DemoApplication app = ctx.getBean(DemoApplication.class);
app.run(args);
} catch (Exception e) {
e.printStackTrace();
}
}
public void run(String... args) throws Exception {
System.out.println(controller);
}
}
Output:
Controller(ip=127.0.0.1, port=3001, name=TestHsm, timeout=1000, provider=software)
This does not work either, it appears. I get an "Error creating bean with name 'defaultValidator'" I also cant use lombok in this project.
– NicO
Nov 26 '18 at 15:10
added code for Controller.java without Lombok, try it
– VietDD
Nov 26 '18 at 15:17
add a comment |
Controller.java
With Lombok
package com.example.demo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hsm")
@Data
public class Controller {
@Value("${hsm.ip}")
private String ip;
@Value("${hsm.port}")
private String port;
@Value("${hsm.name}")
private String name;
@Value("${hsm.timeout}")
private String timeout;
@Value("${hsm.provider}")
private String provider;
}
Without Lombok ( generated by Intelliji - Refactor DeLombok )
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hsm")
public class Controller {
@Value("${hsm.ip}")
private String ip;
@Value("${hsm.port}")
private String port;
@Value("${hsm.name}")
private String name;
@Value("${hsm.timeout}")
private String timeout;
@Value("${hsm.provider}")
private String provider;
public Controller() {
}
public String getIp() {
return this.ip;
}
public String getPort() {
return this.port;
}
public String getName() {
return this.name;
}
public String getTimeout() {
return this.timeout;
}
public String getProvider() {
return this.provider;
}
public void setIp(String ip) {
this.ip = ip;
}
public void setPort(String port) {
this.port = port;
}
public void setName(String name) {
this.name = name;
}
public void setTimeout(String timeout) {
this.timeout = timeout;
}
public void setProvider(String provider) {
this.provider = provider;
}
public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof Controller)) return false;
final Controller other = (Controller) o;
if (!other.canEqual((Object) this)) return false;
final Object this$ip = this.getIp();
final Object other$ip = other.getIp();
if (this$ip == null ? other$ip != null : !this$ip.equals(other$ip)) return false;
final Object this$port = this.getPort();
final Object other$port = other.getPort();
if (this$port == null ? other$port != null : !this$port.equals(other$port)) return false;
final Object this$name = this.getName();
final Object other$name = other.getName();
if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
final Object this$timeout = this.getTimeout();
final Object other$timeout = other.getTimeout();
if (this$timeout == null ? other$timeout != null : !this$timeout.equals(other$timeout)) return false;
final Object this$provider = this.getProvider();
final Object other$provider = other.getProvider();
if (this$provider == null ? other$provider != null : !this$provider.equals(other$provider)) return false;
return true;
}
protected boolean canEqual(final Object other) {
return other instanceof Controller;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $ip = this.getIp();
result = result * PRIME + ($ip == null ? 43 : $ip.hashCode());
final Object $port = this.getPort();
result = result * PRIME + ($port == null ? 43 : $port.hashCode());
final Object $name = this.getName();
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
final Object $timeout = this.getTimeout();
result = result * PRIME + ($timeout == null ? 43 : $timeout.hashCode());
final Object $provider = this.getProvider();
result = result * PRIME + ($provider == null ? 43 : $provider.hashCode());
return result;
}
public String toString() {
return "Controller(ip=" + this.getIp() + ", port=" + this.getPort() + ", name=" + this.getName() + ", timeout=" + this.getTimeout() + ", provider=" + this.getProvider() + ")";
}
}
DemoApplication.java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class DemoApplication {
@Autowired
Controller controller;
public static void main(String args) {
try (ConfigurableApplicationContext ctx = SpringApplication.run(DemoApplication.class, args)) {
DemoApplication app = ctx.getBean(DemoApplication.class);
app.run(args);
} catch (Exception e) {
e.printStackTrace();
}
}
public void run(String... args) throws Exception {
System.out.println(controller);
}
}
Output:
Controller(ip=127.0.0.1, port=3001, name=TestHsm, timeout=1000, provider=software)
This does not work either, it appears. I get an "Error creating bean with name 'defaultValidator'" I also cant use lombok in this project.
– NicO
Nov 26 '18 at 15:10
added code for Controller.java without Lombok, try it
– VietDD
Nov 26 '18 at 15:17
add a comment |
Controller.java
With Lombok
package com.example.demo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hsm")
@Data
public class Controller {
@Value("${hsm.ip}")
private String ip;
@Value("${hsm.port}")
private String port;
@Value("${hsm.name}")
private String name;
@Value("${hsm.timeout}")
private String timeout;
@Value("${hsm.provider}")
private String provider;
}
Without Lombok ( generated by Intelliji - Refactor DeLombok )
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hsm")
public class Controller {
@Value("${hsm.ip}")
private String ip;
@Value("${hsm.port}")
private String port;
@Value("${hsm.name}")
private String name;
@Value("${hsm.timeout}")
private String timeout;
@Value("${hsm.provider}")
private String provider;
public Controller() {
}
public String getIp() {
return this.ip;
}
public String getPort() {
return this.port;
}
public String getName() {
return this.name;
}
public String getTimeout() {
return this.timeout;
}
public String getProvider() {
return this.provider;
}
public void setIp(String ip) {
this.ip = ip;
}
public void setPort(String port) {
this.port = port;
}
public void setName(String name) {
this.name = name;
}
public void setTimeout(String timeout) {
this.timeout = timeout;
}
public void setProvider(String provider) {
this.provider = provider;
}
public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof Controller)) return false;
final Controller other = (Controller) o;
if (!other.canEqual((Object) this)) return false;
final Object this$ip = this.getIp();
final Object other$ip = other.getIp();
if (this$ip == null ? other$ip != null : !this$ip.equals(other$ip)) return false;
final Object this$port = this.getPort();
final Object other$port = other.getPort();
if (this$port == null ? other$port != null : !this$port.equals(other$port)) return false;
final Object this$name = this.getName();
final Object other$name = other.getName();
if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
final Object this$timeout = this.getTimeout();
final Object other$timeout = other.getTimeout();
if (this$timeout == null ? other$timeout != null : !this$timeout.equals(other$timeout)) return false;
final Object this$provider = this.getProvider();
final Object other$provider = other.getProvider();
if (this$provider == null ? other$provider != null : !this$provider.equals(other$provider)) return false;
return true;
}
protected boolean canEqual(final Object other) {
return other instanceof Controller;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $ip = this.getIp();
result = result * PRIME + ($ip == null ? 43 : $ip.hashCode());
final Object $port = this.getPort();
result = result * PRIME + ($port == null ? 43 : $port.hashCode());
final Object $name = this.getName();
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
final Object $timeout = this.getTimeout();
result = result * PRIME + ($timeout == null ? 43 : $timeout.hashCode());
final Object $provider = this.getProvider();
result = result * PRIME + ($provider == null ? 43 : $provider.hashCode());
return result;
}
public String toString() {
return "Controller(ip=" + this.getIp() + ", port=" + this.getPort() + ", name=" + this.getName() + ", timeout=" + this.getTimeout() + ", provider=" + this.getProvider() + ")";
}
}
DemoApplication.java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class DemoApplication {
@Autowired
Controller controller;
public static void main(String args) {
try (ConfigurableApplicationContext ctx = SpringApplication.run(DemoApplication.class, args)) {
DemoApplication app = ctx.getBean(DemoApplication.class);
app.run(args);
} catch (Exception e) {
e.printStackTrace();
}
}
public void run(String... args) throws Exception {
System.out.println(controller);
}
}
Output:
Controller(ip=127.0.0.1, port=3001, name=TestHsm, timeout=1000, provider=software)
Controller.java
With Lombok
package com.example.demo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hsm")
@Data
public class Controller {
@Value("${hsm.ip}")
private String ip;
@Value("${hsm.port}")
private String port;
@Value("${hsm.name}")
private String name;
@Value("${hsm.timeout}")
private String timeout;
@Value("${hsm.provider}")
private String provider;
}
Without Lombok ( generated by Intelliji - Refactor DeLombok )
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hsm")
public class Controller {
@Value("${hsm.ip}")
private String ip;
@Value("${hsm.port}")
private String port;
@Value("${hsm.name}")
private String name;
@Value("${hsm.timeout}")
private String timeout;
@Value("${hsm.provider}")
private String provider;
public Controller() {
}
public String getIp() {
return this.ip;
}
public String getPort() {
return this.port;
}
public String getName() {
return this.name;
}
public String getTimeout() {
return this.timeout;
}
public String getProvider() {
return this.provider;
}
public void setIp(String ip) {
this.ip = ip;
}
public void setPort(String port) {
this.port = port;
}
public void setName(String name) {
this.name = name;
}
public void setTimeout(String timeout) {
this.timeout = timeout;
}
public void setProvider(String provider) {
this.provider = provider;
}
public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof Controller)) return false;
final Controller other = (Controller) o;
if (!other.canEqual((Object) this)) return false;
final Object this$ip = this.getIp();
final Object other$ip = other.getIp();
if (this$ip == null ? other$ip != null : !this$ip.equals(other$ip)) return false;
final Object this$port = this.getPort();
final Object other$port = other.getPort();
if (this$port == null ? other$port != null : !this$port.equals(other$port)) return false;
final Object this$name = this.getName();
final Object other$name = other.getName();
if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
final Object this$timeout = this.getTimeout();
final Object other$timeout = other.getTimeout();
if (this$timeout == null ? other$timeout != null : !this$timeout.equals(other$timeout)) return false;
final Object this$provider = this.getProvider();
final Object other$provider = other.getProvider();
if (this$provider == null ? other$provider != null : !this$provider.equals(other$provider)) return false;
return true;
}
protected boolean canEqual(final Object other) {
return other instanceof Controller;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $ip = this.getIp();
result = result * PRIME + ($ip == null ? 43 : $ip.hashCode());
final Object $port = this.getPort();
result = result * PRIME + ($port == null ? 43 : $port.hashCode());
final Object $name = this.getName();
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
final Object $timeout = this.getTimeout();
result = result * PRIME + ($timeout == null ? 43 : $timeout.hashCode());
final Object $provider = this.getProvider();
result = result * PRIME + ($provider == null ? 43 : $provider.hashCode());
return result;
}
public String toString() {
return "Controller(ip=" + this.getIp() + ", port=" + this.getPort() + ", name=" + this.getName() + ", timeout=" + this.getTimeout() + ", provider=" + this.getProvider() + ")";
}
}
DemoApplication.java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class DemoApplication {
@Autowired
Controller controller;
public static void main(String args) {
try (ConfigurableApplicationContext ctx = SpringApplication.run(DemoApplication.class, args)) {
DemoApplication app = ctx.getBean(DemoApplication.class);
app.run(args);
} catch (Exception e) {
e.printStackTrace();
}
}
public void run(String... args) throws Exception {
System.out.println(controller);
}
}
Output:
Controller(ip=127.0.0.1, port=3001, name=TestHsm, timeout=1000, provider=software)
edited Nov 26 '18 at 15:43
answered Nov 26 '18 at 14:52
VietDDVietDD
36828
36828
This does not work either, it appears. I get an "Error creating bean with name 'defaultValidator'" I also cant use lombok in this project.
– NicO
Nov 26 '18 at 15:10
added code for Controller.java without Lombok, try it
– VietDD
Nov 26 '18 at 15:17
add a comment |
This does not work either, it appears. I get an "Error creating bean with name 'defaultValidator'" I also cant use lombok in this project.
– NicO
Nov 26 '18 at 15:10
added code for Controller.java without Lombok, try it
– VietDD
Nov 26 '18 at 15:17
This does not work either, it appears. I get an "Error creating bean with name 'defaultValidator'" I also cant use lombok in this project.
– NicO
Nov 26 '18 at 15:10
This does not work either, it appears. I get an "Error creating bean with name 'defaultValidator'" I also cant use lombok in this project.
– NicO
Nov 26 '18 at 15:10
added code for Controller.java without Lombok, try it
– VietDD
Nov 26 '18 at 15:17
added code for Controller.java without Lombok, try it
– VietDD
Nov 26 '18 at 15:17
add a comment |
Okay, i solved it with the top answer of this Question. I put the variables and @Values in the signature of the constructor and not as class variables.
add a comment |
Okay, i solved it with the top answer of this Question. I put the variables and @Values in the signature of the constructor and not as class variables.
add a comment |
Okay, i solved it with the top answer of this Question. I put the variables and @Values in the signature of the constructor and not as class variables.
Okay, i solved it with the top answer of this Question. I put the variables and @Values in the signature of the constructor and not as class variables.
answered Feb 27 at 14:03
NicONicO
287
287
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53482633%2fvalue-in-springboot-returns-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Probably your controller class is not picked up by your application scanning. Could you show us your package structure?
– Urosh T.
Nov 26 '18 at 14:02
apllication.properties
are you sure you have not a typo here? should beapplication.properties
– pleft
Nov 26 '18 at 14:04
@pleft woops, thats a typo in the question, the one i actually have in the project is correct tho.
– NicO
Nov 26 '18 at 14:14
@UroshT. ive added it
– NicO
Nov 26 '18 at 14:17
Check out SpringBoot's configuration proeprties to bind external properties to an Object that can be passed. Its cleaner than using @Value everwhere and should by proxy fix your issue. The controller may be being bean'd before the environment has fully refreshed with the properties.
– Darren Forsythe
Nov 26 '18 at 15:56