http
.When using it, it is necessary to be aware of the protocol. Be especially careful if the protocol is different for each environment (development, staging, production).
TestContoroller.java
public class TestController {
@GetMapping("/show")
public ModelAndView test() {
return new ModelAndView("redirect:" + "/sample");
}
}
In the above case
It becomes http: // localhost / sample
.
If you want to read this from application.yml and set it
TestContoroller.java
@RequiredArgsConstructor
@EnableConfigurationProperties({RedirectUrlProperties.class})
public class TestController {
private final RedirectUrlProperties redirectUrlProperties;
@GetMapping("/show")
public ModelAndView test() {
return new ModelAndView("redirect:" + buildAdminRedirectUrl("/sample"));
}
/*
*spring redirect is http by default
*Method to create redirectURL for each environment
*/
public String buildAdminRedirectUrl(String url) {
return UriComponentsBuilder.fromUriString(redirectUrlProperties.getUrl() + url).toUriString();
}
}
application.yml
spring:
redirect:
url: https://localhost
RedirectUrlProperties.java
@Getter
@Setter
@ConfigurationProperties(prefix= "spring.redirect")
public class RedirectUrlProperties {
private String url;
}
So far we've described redirect
, but if you just want to hit an internal method, consider using forward
.
However, since the URL of forward
does not change, it is necessary to verify whether it is suitable including that side.
(redirect throws the request again, but forward processes it internally, so the request is only the first time)
I referred to the following. https://qiita.com/rubytomato@github/items/8d132dec042f695e50f6
Recommended Posts