Spring Cloud Openfeign Get Request 405 Error [How to Solve]

KUST retrieve service

@Resource
private AuthFeignService authFeignService;

@ApiOperation("Obtain user information")
@GetMapping("/get/user")
public ResponseEntity<UserMsgDTO> getUserName(HttpServletRequest request){
    String token = request.getHeader("token");
    UserMsgDTO userMsg = authFeignService.getUserMsg(token);
    log.info("userMsg -> {}", userMsg);
    return Results.success(userMsg);
}

@FeignClient("kust-auth")
public interface AuthFeignService {
    @GetMapping("/auth/get/user")
    UserMsgDTO getUserMsg(String jwtToken);
}

KUST auth service

@ApiOperation("Get the username information in the token")
@GetMapping("/get/user")
public UserMsgDTO getUserMsg(String jwtToken){
    log.info("jwtToken -> {}", jwtToken);
    return JwtUtils.getUSerNameByJwtToken(jwtToken);
}

The error reported by the KUST retrieve service is as follows:

feign.FeignException$MethodNotAllowed: status 405 reading AuthFeignService#getUserMsg(String)

The error reported by the KUST auth service is as follows:

Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]

Reason: The location of the request parameter is not specified in the feign interface, the parameter “String jwtToken” will be encapsulated in the request body by default, feign will check if the request body is empty when passing the request, if not, the Get request will be converted to Post, and the method in the accepted service kust-auth is Get, so it will naturally report an error 405
Solution is as follows: use @RequestParam(“jwtToken”) to clarify the location of the parameters, note: the string in brackets must be written, or start the error

@FeignClient("kust-auth")
public interface AuthFeignService {
    @GetMapping("/auth/get/user")
    UserMsgDTO getUserMsg(@RequestParam("jwtToken") String jwtToken);
}

Then restart and you can get the data

2022-01-25 11:28:43.577 INFO 16236 --- [nio-9001-exec-9] com. coast. search. api. v1. TokenController: userMsg --gt; UserMsgDTO(id=4, name=LIUXIAOPANG)

Similar Posts: