New to Spring/SpringBoot, I have two methods like bellow -
1.
@GetMapping("/score")
public UserScoreRes getUserScore(@RequestHeader("api-key") String apiKey,
@RequestHeader("jwt") String jwt,
@RequestParam(name = "date", defaultValue = "2022-07-15") String dateStr) {
.....
}
@GetMapping(value = "/score", params ="behaviour-version=2")
public UserScoreResV2 getUserScoreV2(@RequestHeader("api-key") String apiKey,
@RequestHeader("jwt") String jwt,
@RequestParam(name = "date", defaultValue = "2022-07-15") String dateStr) {
....
}
In method 2 (getUserScoreV2
), how can I get behaviour-version
inside the method?
Tried with -
@GetMapping(value = "/score", params ="behaviour-version=2")
public UserScoreResV2 getUserScoreV2(@RequestHeader("api-key") String apiKey,
@RequestHeader("jwt") String jwt,
@RequestParam(name = "behaviour-version", required = false) String behaviourVersion,
@RequestParam(name = "date", defaultValue = "2022-07-15") String dateStr) {
....
}
but, it is not working.
Can not modify method 1 (getUserScore
) because a version is released in production with that.
CodePudding user response:
try this
@GetMapping(value = "/score", params ="behaviour-version=2")
public UserScoreResV2 getUserScoreV2(@RequestHeader("api-key") String apiKey,
@RequestHeader("jwt") String jwt,
@RequestParam(name = "date", defaultValue = "2022-07-15") String dateStr,
ServletRequest request) {
String behaviorVersion = request.getParameter("behaviour-version");
}
warning
As Deinum mentioned, behavior-version should be 2.
Otherwise, you can't send request. The server will return 400 error
CodePudding user response:
You can do this using handler interceptor. What you have to do is when application recieve a request for /score
endpoint with behaviour_version
header or param you have to redirect that to the another endpoint through preHandler (ex: /score/v2
).
Then caller can send request for same endpoint with additional header/param. Redirecting according to header do by springboot interceptor.
Ex handler inteceptor:
public class ScoreInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
if(request.getParameter(behaviour_v)=="2"){
response.redirect('/score/v1');
return false;
}
return true;
}
}