@GetMapping with array parameters by application.yml












3















I have developed this @GetMapping RestController and all works fine



@GetMapping(path = {"foo", "bar"})
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}


now I want externalize the values inside the path array using my application.yml file,so I writed



url:
- foo
- bar


and I modified my code in order to use it, but it doesn't work in this two different ways



@GetMapping(path = "${url}")
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}

@GetMapping(path = {"${url}"})
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}


I don't understand if the application properties are not correctly formatted or I need to use the SpEL (https://docs.spring.io/spring/docs/3.0.x/reference/expressions.html.



I also want that the code is dynamic according to the application.yml properties, so if the url values increase or decrease the code must still work.



I'm using Springboot 1.5.13










share|improve this question

























  • You should try @ConfigurationProperties annotation to fetch yml configuration data.

    – GauravRai1512
    Nov 16 '18 at 11:33











  • @GauravRai1512 and after that? How I can use the fetched properties and inject it in the @GetMapping annotation? You cannot inject variable in the annotation, only constant

    – Federico Gatti
    Nov 16 '18 at 11:38
















3















I have developed this @GetMapping RestController and all works fine



@GetMapping(path = {"foo", "bar"})
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}


now I want externalize the values inside the path array using my application.yml file,so I writed



url:
- foo
- bar


and I modified my code in order to use it, but it doesn't work in this two different ways



@GetMapping(path = "${url}")
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}

@GetMapping(path = {"${url}"})
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}


I don't understand if the application properties are not correctly formatted or I need to use the SpEL (https://docs.spring.io/spring/docs/3.0.x/reference/expressions.html.



I also want that the code is dynamic according to the application.yml properties, so if the url values increase or decrease the code must still work.



I'm using Springboot 1.5.13










share|improve this question

























  • You should try @ConfigurationProperties annotation to fetch yml configuration data.

    – GauravRai1512
    Nov 16 '18 at 11:33











  • @GauravRai1512 and after that? How I can use the fetched properties and inject it in the @GetMapping annotation? You cannot inject variable in the annotation, only constant

    – Federico Gatti
    Nov 16 '18 at 11:38














3












3








3








I have developed this @GetMapping RestController and all works fine



@GetMapping(path = {"foo", "bar"})
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}


now I want externalize the values inside the path array using my application.yml file,so I writed



url:
- foo
- bar


and I modified my code in order to use it, but it doesn't work in this two different ways



@GetMapping(path = "${url}")
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}

@GetMapping(path = {"${url}"})
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}


I don't understand if the application properties are not correctly formatted or I need to use the SpEL (https://docs.spring.io/spring/docs/3.0.x/reference/expressions.html.



I also want that the code is dynamic according to the application.yml properties, so if the url values increase or decrease the code must still work.



I'm using Springboot 1.5.13










share|improve this question
















I have developed this @GetMapping RestController and all works fine



@GetMapping(path = {"foo", "bar"})
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}


now I want externalize the values inside the path array using my application.yml file,so I writed



url:
- foo
- bar


and I modified my code in order to use it, but it doesn't work in this two different ways



@GetMapping(path = "${url}")
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}

@GetMapping(path = {"${url}"})
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}


I don't understand if the application properties are not correctly formatted or I need to use the SpEL (https://docs.spring.io/spring/docs/3.0.x/reference/expressions.html.



I also want that the code is dynamic according to the application.yml properties, so if the url values increase or decrease the code must still work.



I'm using Springboot 1.5.13







java spring spring-boot spring-restcontroller






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 13:02







Federico Gatti

















asked Nov 16 '18 at 11:30









Federico GattiFederico Gatti

152114




152114













  • You should try @ConfigurationProperties annotation to fetch yml configuration data.

    – GauravRai1512
    Nov 16 '18 at 11:33











  • @GauravRai1512 and after that? How I can use the fetched properties and inject it in the @GetMapping annotation? You cannot inject variable in the annotation, only constant

    – Federico Gatti
    Nov 16 '18 at 11:38



















  • You should try @ConfigurationProperties annotation to fetch yml configuration data.

    – GauravRai1512
    Nov 16 '18 at 11:33











  • @GauravRai1512 and after that? How I can use the fetched properties and inject it in the @GetMapping annotation? You cannot inject variable in the annotation, only constant

    – Federico Gatti
    Nov 16 '18 at 11:38

















You should try @ConfigurationProperties annotation to fetch yml configuration data.

– GauravRai1512
Nov 16 '18 at 11:33





You should try @ConfigurationProperties annotation to fetch yml configuration data.

– GauravRai1512
Nov 16 '18 at 11:33













@GauravRai1512 and after that? How I can use the fetched properties and inject it in the @GetMapping annotation? You cannot inject variable in the annotation, only constant

– Federico Gatti
Nov 16 '18 at 11:38





@GauravRai1512 and after that? How I can use the fetched properties and inject it in the @GetMapping annotation? You cannot inject variable in the annotation, only constant

– Federico Gatti
Nov 16 '18 at 11:38












2 Answers
2






active

oldest

votes


















2














You can use in your controller



@GetMapping(path = "${url[0]}")
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}

@GetMapping(path = {"${url[1]}"})
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}


Or you can do in this way:



@GetMapping(path = {"${url[0]}","${url[1]}"})
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}


I think this is helpful






share|improve this answer
























  • Thank you, your second solutions work fine with a specific number of url in the properties, I'm searching for a dynamic solution for any number of values inside the array

    – Federico Gatti
    Nov 16 '18 at 11:55





















1














You can't bind YAML list to array or list here. For more information see: @Value and @ConfigurationProperties behave differently when binding to arrays



However, you can achieve this by specifying regular expression in yml file like:



url: '{var:foo|bar}'


And then you can use it directly in your controller:



@GetMapping(path = "${url}")
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}





share|improve this answer


























  • Your solution doesn't work because you have only defined a PathVariable that is called foo|bar so the endpoint is matched by all the call like (localhost/anytypeofrequest)

    – Federico Gatti
    Nov 19 '18 at 9:39













  • @FedericoGatti Thanks, missed it. Edited answer.

    – Sukhpal Singh
    Nov 19 '18 at 10:55











  • Thank you, in this way is possible to define a PathVaribale only for one of the two urls? Something like url: '{var:foo/{foobar}|bar}'

    – Federico Gatti
    Nov 19 '18 at 11:55











  • @FedericoGatti var is name of your @PathVariable. I think this is what you need.

    – Sukhpal Singh
    Nov 19 '18 at 12:21












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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336985%2fgetmapping-with-array-parameters-by-application-yml%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can use in your controller



@GetMapping(path = "${url[0]}")
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}

@GetMapping(path = {"${url[1]}"})
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}


Or you can do in this way:



@GetMapping(path = {"${url[0]}","${url[1]}"})
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}


I think this is helpful






share|improve this answer
























  • Thank you, your second solutions work fine with a specific number of url in the properties, I'm searching for a dynamic solution for any number of values inside the array

    – Federico Gatti
    Nov 16 '18 at 11:55


















2














You can use in your controller



@GetMapping(path = "${url[0]}")
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}

@GetMapping(path = {"${url[1]}"})
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}


Or you can do in this way:



@GetMapping(path = {"${url[0]}","${url[1]}"})
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}


I think this is helpful






share|improve this answer
























  • Thank you, your second solutions work fine with a specific number of url in the properties, I'm searching for a dynamic solution for any number of values inside the array

    – Federico Gatti
    Nov 16 '18 at 11:55
















2












2








2







You can use in your controller



@GetMapping(path = "${url[0]}")
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}

@GetMapping(path = {"${url[1]}"})
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}


Or you can do in this way:



@GetMapping(path = {"${url[0]}","${url[1]}"})
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}


I think this is helpful






share|improve this answer













You can use in your controller



@GetMapping(path = "${url[0]}")
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}

@GetMapping(path = {"${url[1]}"})
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}


Or you can do in this way:



@GetMapping(path = {"${url[0]}","${url[1]}"})
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}


I think this is helpful







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 11:39









Mykhailo MoskuraMykhailo Moskura

877214




877214













  • Thank you, your second solutions work fine with a specific number of url in the properties, I'm searching for a dynamic solution for any number of values inside the array

    – Federico Gatti
    Nov 16 '18 at 11:55





















  • Thank you, your second solutions work fine with a specific number of url in the properties, I'm searching for a dynamic solution for any number of values inside the array

    – Federico Gatti
    Nov 16 '18 at 11:55



















Thank you, your second solutions work fine with a specific number of url in the properties, I'm searching for a dynamic solution for any number of values inside the array

– Federico Gatti
Nov 16 '18 at 11:55







Thank you, your second solutions work fine with a specific number of url in the properties, I'm searching for a dynamic solution for any number of values inside the array

– Federico Gatti
Nov 16 '18 at 11:55















1














You can't bind YAML list to array or list here. For more information see: @Value and @ConfigurationProperties behave differently when binding to arrays



However, you can achieve this by specifying regular expression in yml file like:



url: '{var:foo|bar}'


And then you can use it directly in your controller:



@GetMapping(path = "${url}")
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}





share|improve this answer


























  • Your solution doesn't work because you have only defined a PathVariable that is called foo|bar so the endpoint is matched by all the call like (localhost/anytypeofrequest)

    – Federico Gatti
    Nov 19 '18 at 9:39













  • @FedericoGatti Thanks, missed it. Edited answer.

    – Sukhpal Singh
    Nov 19 '18 at 10:55











  • Thank you, in this way is possible to define a PathVaribale only for one of the two urls? Something like url: '{var:foo/{foobar}|bar}'

    – Federico Gatti
    Nov 19 '18 at 11:55











  • @FedericoGatti var is name of your @PathVariable. I think this is what you need.

    – Sukhpal Singh
    Nov 19 '18 at 12:21
















1














You can't bind YAML list to array or list here. For more information see: @Value and @ConfigurationProperties behave differently when binding to arrays



However, you can achieve this by specifying regular expression in yml file like:



url: '{var:foo|bar}'


And then you can use it directly in your controller:



@GetMapping(path = "${url}")
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}





share|improve this answer


























  • Your solution doesn't work because you have only defined a PathVariable that is called foo|bar so the endpoint is matched by all the call like (localhost/anytypeofrequest)

    – Federico Gatti
    Nov 19 '18 at 9:39













  • @FedericoGatti Thanks, missed it. Edited answer.

    – Sukhpal Singh
    Nov 19 '18 at 10:55











  • Thank you, in this way is possible to define a PathVaribale only for one of the two urls? Something like url: '{var:foo/{foobar}|bar}'

    – Federico Gatti
    Nov 19 '18 at 11:55











  • @FedericoGatti var is name of your @PathVariable. I think this is what you need.

    – Sukhpal Singh
    Nov 19 '18 at 12:21














1












1








1







You can't bind YAML list to array or list here. For more information see: @Value and @ConfigurationProperties behave differently when binding to arrays



However, you can achieve this by specifying regular expression in yml file like:



url: '{var:foo|bar}'


And then you can use it directly in your controller:



@GetMapping(path = "${url}")
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}





share|improve this answer















You can't bind YAML list to array or list here. For more information see: @Value and @ConfigurationProperties behave differently when binding to arrays



However, you can achieve this by specifying regular expression in yml file like:



url: '{var:foo|bar}'


And then you can use it directly in your controller:



@GetMapping(path = "${url}")
public ResponseEntity<String> foobar() {
return ResponseEntity.ok("foobar");
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 10:54

























answered Nov 18 '18 at 13:48









Sukhpal SinghSukhpal Singh

1,039212




1,039212













  • Your solution doesn't work because you have only defined a PathVariable that is called foo|bar so the endpoint is matched by all the call like (localhost/anytypeofrequest)

    – Federico Gatti
    Nov 19 '18 at 9:39













  • @FedericoGatti Thanks, missed it. Edited answer.

    – Sukhpal Singh
    Nov 19 '18 at 10:55











  • Thank you, in this way is possible to define a PathVaribale only for one of the two urls? Something like url: '{var:foo/{foobar}|bar}'

    – Federico Gatti
    Nov 19 '18 at 11:55











  • @FedericoGatti var is name of your @PathVariable. I think this is what you need.

    – Sukhpal Singh
    Nov 19 '18 at 12:21



















  • Your solution doesn't work because you have only defined a PathVariable that is called foo|bar so the endpoint is matched by all the call like (localhost/anytypeofrequest)

    – Federico Gatti
    Nov 19 '18 at 9:39













  • @FedericoGatti Thanks, missed it. Edited answer.

    – Sukhpal Singh
    Nov 19 '18 at 10:55











  • Thank you, in this way is possible to define a PathVaribale only for one of the two urls? Something like url: '{var:foo/{foobar}|bar}'

    – Federico Gatti
    Nov 19 '18 at 11:55











  • @FedericoGatti var is name of your @PathVariable. I think this is what you need.

    – Sukhpal Singh
    Nov 19 '18 at 12:21

















Your solution doesn't work because you have only defined a PathVariable that is called foo|bar so the endpoint is matched by all the call like (localhost/anytypeofrequest)

– Federico Gatti
Nov 19 '18 at 9:39







Your solution doesn't work because you have only defined a PathVariable that is called foo|bar so the endpoint is matched by all the call like (localhost/anytypeofrequest)

– Federico Gatti
Nov 19 '18 at 9:39















@FedericoGatti Thanks, missed it. Edited answer.

– Sukhpal Singh
Nov 19 '18 at 10:55





@FedericoGatti Thanks, missed it. Edited answer.

– Sukhpal Singh
Nov 19 '18 at 10:55













Thank you, in this way is possible to define a PathVaribale only for one of the two urls? Something like url: '{var:foo/{foobar}|bar}'

– Federico Gatti
Nov 19 '18 at 11:55





Thank you, in this way is possible to define a PathVaribale only for one of the two urls? Something like url: '{var:foo/{foobar}|bar}'

– Federico Gatti
Nov 19 '18 at 11:55













@FedericoGatti var is name of your @PathVariable. I think this is what you need.

– Sukhpal Singh
Nov 19 '18 at 12:21





@FedericoGatti var is name of your @PathVariable. I think this is what you need.

– Sukhpal Singh
Nov 19 '18 at 12:21


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336985%2fgetmapping-with-array-parameters-by-application-yml%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Bressuire

Vorschmack

Quarantine