I have this piece of code working fine on a project that uses RestTemplateBuilder 1.5.14
this.restTemplate = restTemplateBuilder
.setConnectTimeout(connectTimeout)
.setReadTimeout(readTimeout)
.requestFactory(new MyHttpComponentFactoryBuilder()
.build())
.build();
Run Code Online (Sandbox Code Playgroud)
After updating to RestTemplateBuilder 2.1.5
I have this piece of code:
this.restTemplate = restTemplateBuilder
.setConnectTimeout(Duration.ofMillis(connectTimeout))
.setReadTimeout(Duration.ofMillis(readTimeout))
.requestFactory(new MyHttpComponentFactoryBuilder().build().getClass())
.build();
Run Code Online (Sandbox Code Playgroud)
but when running the code I have a InvocationTargetException / NullPointerException
that dissapears when deleting the line .requestFactory(new MyHttpComponentFactoryBuilder().build().getClass())
, but debugging new MyHttpComponentFactoryBuilder().build().getClass()
is not null
I also tried with the solution proposed:
...
.requestFactory(new MyRequestFactorySupplier()) …
Run Code Online (Sandbox Code Playgroud) 是否可以在Spring批处理项目中使用RestAPI(REST TEMPLATE)从DB读取数据,处理它并在ItemWriter中发送到另一个系统?我所能看到的就是获取数据并将其写入 csv 文件。
spring-batch spring-boot spring-rest itemwriter spring-resttemplate
我迁移到 spring boot 3 和 java 21。由于我要进行补丁休息调用,因此我需要依赖项 org.apache.httpcomponents.client5:httpclient5:5.2.2。
但有了这种依赖性,如果将 org.springframework.web.client.RestTemplate 与 RequestEntity.get 一起使用,我会得到“java.lang.IllegalArgumentException:无效代理”。
在相同的设置下,补丁调用工作得很好。
有谁知道为什么会发生这种情况?我没有代理。我必须定义默认代理吗?
不幸的是,对于此链接:https://github.com/apache/httpcomponents-client/tree/5.1.x/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientConfiguration.java 我得到 404。:-(
我尝试仅使用 apache 依赖项而不使用 Resttemplate 并得到相同的异常。
HttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(uri);
httpGet.setHeader(HttpHeaders.AUTHORIZATION, token);
httpGet.setHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
Run Code Online (Sandbox Code Playgroud) apache-httpclient-5.x spring-resttemplate java-21 spring-boot-3
I am working on a Spring Boot project using RestTemplate in order to perform a POST request toward an URL containing query parameter
This is my code:
@Override
public boolean insertNotaryDistrictDetailsAsPost(NotaryDistrictDetails notaryDistrict) {
HttpHeaders basicAuthHeader = this.getWpBasicAuthenticationHeader();
HttpEntity<String> request = new HttpEntity<String>(basicAuthHeader);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(wpPortalWriteNotaryDistrictDetailsAsPostUrl)
// BASIC INFO:
.queryParam("title", notaryDistrict.getDistretto())
.queryParam("wpcf-distretto-notary-district", notaryDistrict.getDistretto())
.queryParam("wpcf-indirizzo-notary-district", notaryDistrict.getIndirizzo())
.queryParam("wpcf-denominazione-notary-district", notaryDistrict.getDenominazione())
.queryParam("wpcf-provincia-notary-district", notaryDistrict.getProvincia())
.queryParam("wpcf-regione-notary-district", notaryDistrict.getRegione())
.queryParam("wpcf-cap-notary-district", notaryDistrict.getCap())
.queryParam("wpcf-telefono-notary-district", notaryDistrict.getTelefono())
.queryParam("wpcf-fax-notary-district", notaryDistrict.getFax())
.queryParam("wpcf-email-notary-district", notaryDistrict.getEmail())
.queryParam("wpcf-pec-notary-district", notaryDistrict.getPec())
.queryParam("wpcf-web-url-notary-district", notaryDistrict.getWebUrl());
ResponseEntity<String> response = restTemplate.exchange(builder.toUriString(),
HttpMethod.POST,
request,
String.class); …
Run Code Online (Sandbox Code Playgroud) 为什么我的集成测试抱怨缺少RestTemplate
接线Spring Boot
?
我可以做什么来修复这个集成测试?
此外,这是集成测试还是单元测试?根据我的理解,这个测试是调用一个模拟服务器,它是一个外部实体(在被测类之外),因此它是一个集成测试。我的理解正确吗?
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath />
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
Run Code Online (Sandbox Code Playgroud)
src/main/java/RestTemplateConfig.java
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
Collection<ClientHttpRequestInterceptor> clientHttpRequestInterceptors = List.of(new RestTemplateInterceptor());
return new RestTemplateBuilder()
.errorHandler(new CustomClientErrorHandler())
.interceptors(clientHttpRequestInterceptors)
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
src/main/java/AccountService.java
@Service
@Log4j2
public class AccountService {
@Autowired
private RestTemplate restTemplate;
public Account getAccount() throws Exception {
//build uri
URI uri = new URI(http://api.demo.com/api/account)
log.info("uri: " + uri); …
Run Code Online (Sandbox Code Playgroud) spring spring-test spring-boot spring-rest spring-resttemplate
可以在HttpHeader中设置“application/x-www-form-urlencoded”,但我想为requestbody设置,你能指导一下吗?
示例 json:
"header": [
{
"key": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "username",
"value": "Tohid",
"type": "text"
},
{
"key": "password",
"value": "*mk",
"type": "text"
},
{
"key": "grant_type",
"value": "password",
"type": "text"
}
]
},
Run Code Online (Sandbox Code Playgroud)
代码 :
HttpHeaders headers = new HttpHeaders();
headers.add(MediaType.APPLICATION_JSON, APPLICATION_URLENCODED.getValue());
HttpEntity<?> requestEntity = new HttpEntity<>(gson.toJson(requestBody), headers);
Run Code Online (Sandbox Code Playgroud)
我面临以下问题:
我正在使用一个简单的方法调用另一个服务org.springframework.web.client.RestTemplate
调用它时,我需要拦截请求,修改请求正文,并让它继续流程。到目前为止,我没有遇到任何问题,因为org.springframework.http.client.ClientHttpRequestInterceptor
我可以对我的请求执行任何我想要的操作(在将其发送到我正在调用的服务之前将 RequestObjectA 转换为 requestObjectB)。
问题:如何修改响应体?
我看到调用时ClientHttpResponse execute = clientHttpRequestExecution.execute(httpRequest, body)
我可以让身体做execute.getBody()
,所以我可以修改它,但我没有找到一种方法以某种方式将其设置回来。
有什么办法可以将我修改后的身体设置为ClientHttpResponse
?
我正在尝试编写一个客户端,在其中我将执行没有正文的 POST 方法。插入null是唯一的解决方案吗?例子:
client.exchange("/" + userId + "/logout", HttpMethod.POST, null, String.class);
Run Code Online (Sandbox Code Playgroud) spring-boot ×5
java ×4
spring ×4
resttemplate ×3
spring-rest ×2
itemwriter ×1
java-21 ×1
spring-batch ×1
spring-mvc ×1
spring-test ×1