webclient - 在 HTTP GET 请求上添加查询参数时出现 UnsupportedMediaException?
问题描述
我正在使用 Spring 的 WebClient 来调用第三方 api,并且正在使用 Wiremock 来模拟 API 调用。
我有以下课程
public TokenResponse {
private String responseStatus;
private String failureTrace;
private String token;
}
这是我的 Wiremock 文件:
{
"request": {
"method": "GET",
"urlPattern": "/some-uri",
"headers": {
"Content-Type": {
"equalTo": "application/json"
},
"Accept": {
"equalTo": "application/json"
}
}
},
"response": {
"status": 200,
"jsonBody": {
"responseStatus": "SUCCESS",
"failureTrace": "",
"token": "my token"
},
"headers": {
"Content-Type": "application/json"
}
}
}
当在没有任何查询参数的情况下调用 webclient 时GET /some-uri
,一切正常。我得到一个带有正确令牌的 200。
当我像这样更改我的存根文件时:
{
"request": {
"method": "GET",
"urlPattern": "/some-uri",
"queryParameters": {
"id": {
"equalTo": "myLogin"
}
},
"headers": {
"Content-Type": {
"equalTo": "application/json"
},
"Accept": {
"equalTo": "application/json"
}
}
},
"response": {
"status": 200,
"jsonBody": {
"responseStatus": "SUCCESS",
"failureTrace": "",
"token": "mon token"
},
"headers": {
"Content-Type": "application/json"
}
}
}
我像这样更改uri : GET /some-uri?id=myLogin
,然后我收到以下错误:
404 Not Found from GET http://localhost:8091/some-uri; nested exception is org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'text/plain' not supported for bodyType=fr.dto.TokenResponse)\)
这是我的 Webclient 的设置方式:
protected WebClient client = WebClient.create(this.getBaseUrl());
protected ResponseSpec create(String uri, HttpMethod method){
return client.method(method)
.uri(uri)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.onStatus(HttpStatus::is4xxClientError, this::handle4xxError)
.onStatus(HttpStatus::is4xxClientError, this::handle5xxError);
}
解决方案
推荐阅读
- python - 根据列表显示数据框中的特定列
- twitter-bootstrap - 引导项目在页面加载时折叠
- swiftui - Xcode 12 Beta 3 中的 SwiftUI - 将视图显示为工作表时禁用选择器
- git - 如何在 PhpStorm 中禁用自动获取?
- python - 如何在Python中不区分大小写和确定性地对文本字符串进行排序
- tensorflow - 如何在网格中排列从 CNN 提取的特征以传入 Keras 或 tf 2.0 中的 RNN 编码器?
- laravel - Vue js 和 Laravel 中的多图像上传器
- javascript - React 使用 webpack 在自定义路径下构建 .js 文件
- java - 文件的处理:字节数组和维度
- javascript - 当我们通过 chrome android 通知暂停音频时,howler.js 未检测到暂停事件