客户端使用 FeignClient 调用服务端服务时,报错:Request method ‘POST‘ not supported

客户端使用 FeignClient 调用服务端服务时,报错:Request method ‘POST‘ not supported

修改错误前调用方式:

FeignClient 接口服务

@FeignClient(contextId = "remotePayFormService", value = "payment-service-system")
public interface RemotePayFormService {

    @GetMapping("/platPayForm/platPayFormList")
    R<PlatPayform> platPayFormList(@RquestBody PlatPayForm platPayForm @RequestHeader(SecurityConstants.FROM_SOURCE) String source); 
}

服务端代码代码

@RestController
@RequestMapping("/platPayform")
@Slf4j 
public class PlatePayMainController extends BaseController {
  @Autowired
  private IPlatPayFormService platPayFormService;
  /**
   * 支付集合
   */
  @InnerAuth
  @GetMapping("/platPayFormList")
  public R<PlatPayForm> platFormList(@RequestBody PlatPayForm platPayForm ) {
List
<PlatFormName> list = platPayFormService.selectPlatPayFormList(null);
   if (null != list)
return R.ok(list.get(0));
else
return null;
}
}
}
问题分析【报错】 Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]

  提示:不支持Post请求,客户段使用 FeignClient 端用的是Get请求,服务端用的也为Get请求,为什么会报错不支持Post请求呢?
   如吧客户端和服务端都改成Post请求后,就解决问题了。或使用get方式请求不用@RequestBody 注解 参数传递方式。

  原因:
  因为FeignClient 端的请求参数是  @RequestBody PlatPayForm platPayForm,当把对象转换为json字符串的时候 FeignClient会默认发送Post请求

解决方法
1、如果使用@RequestBody 注解,把客户端与服务端都改为Post请求
2、如果非要用Get传送对象类型参数,直接以对象的形式传参,服务端可直接用以下方式接收参数。

修改后,既:正确请求方式参看如下:

FeignClient 接口服务

@FeignClient(contextId = "remotePayFormService", value = "payment-service-system")
public interface RemotePayFormService {

    @GetMapping("/platPayForm/platPayFormList")
    R<PlatPayform> platFormList(@RequestHeader(SecurityConstants.FROM_SOURCE) String source); }

服务端代码代码

@RestController
@RequestMapping("/platPayForm")
@Slf4j
public class PlatePayMainController extends BaseController {
  @Autowired
private IPlatPayFormService platPayFormService;
  /**
* 支付集合
*/
@InnerAuth
@GetMapping("/platPayFormList")
public R<PlatPayform> platformList() {
      List<PlatFormName> list = platPayFormService.selectPlatPayFormList(null);
      if (null != list)
return R.ok(list.get(0));
else
return null;
}
}
}

 

posted @ 2024-04-24 13:49  东北大亨  阅读(305)  评论(0编辑  收藏  举报