[BAEL-8496] - Moved articles to their own spring-resttemplate module
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
package org.baeldung.web.exception;
|
||||
|
||||
public class NotFoundException extends RuntimeException {
|
||||
}
|
||||
-43
@@ -1,43 +0,0 @@
|
||||
package org.baeldung.web.handler;
|
||||
|
||||
import org.baeldung.web.exception.NotFoundException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.client.ResponseErrorHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Component
|
||||
public class RestTemplateResponseErrorHandler
|
||||
implements ResponseErrorHandler {
|
||||
|
||||
@Override
|
||||
public boolean hasError(ClientHttpResponse httpResponse)
|
||||
throws IOException {
|
||||
|
||||
return (httpResponse
|
||||
.getStatusCode()
|
||||
.series() == HttpStatus.Series.CLIENT_ERROR || httpResponse
|
||||
.getStatusCode()
|
||||
.series() == HttpStatus.Series.SERVER_ERROR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleError(ClientHttpResponse httpResponse)
|
||||
throws IOException {
|
||||
|
||||
if (httpResponse
|
||||
.getStatusCode()
|
||||
.series() == HttpStatus.Series.SERVER_ERROR) {
|
||||
//Handle SERVER_ERROR
|
||||
} else if (httpResponse
|
||||
.getStatusCode()
|
||||
.series() == HttpStatus.Series.CLIENT_ERROR) {
|
||||
//Handle CLIENT_ERROR
|
||||
if (httpResponse.getStatusCode() == HttpStatus.NOT_FOUND) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package org.baeldung.web.model;
|
||||
|
||||
public class Bar {
|
||||
private String id;
|
||||
private String name;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package org.baeldung.web.service;
|
||||
|
||||
import org.baeldung.web.handler.RestTemplateResponseErrorHandler;
|
||||
import org.baeldung.web.model.Bar;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Service
|
||||
public class BarConsumerService {
|
||||
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@Autowired
|
||||
public BarConsumerService(RestTemplateBuilder restTemplateBuilder) {
|
||||
RestTemplate restTemplate = restTemplateBuilder
|
||||
.errorHandler(new RestTemplateResponseErrorHandler())
|
||||
.build();
|
||||
}
|
||||
|
||||
public Bar fetchBarById(String barId) {
|
||||
return restTemplate.getForObject("/bars/4242", Bar.class);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user