The method of getting the status code changes without setting DefaultResponseErrorHandler
The answer was here, but I missed it.
Java : 11 JUnit : 4.12 Spring Boot : 2.1.6
SandboxApplication.java
@SpringBootApplication
public class SandboxApplication {
public static void main(String[] args) {
SpringApplication.run(SandboxApplication.class, args);
}
//Roughly bean registration here
@Bean
public RestTemplate setRestTemplate(){
return new RestTemplate();
}
}
Hoge.java
class Hoge {
String message;
public Hoge(@JsonProperty("message") String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
SampleTest.java
@RunWith(SpringRunner.class)
@SpringBootTest
public class SampleTest {
@Autowired
RestTemplate restTemplate;
@Autowired
ObjectMapper objectMapper;
@Autowired
Sample sut;
@Test
public void test() throws Exception {
//2xx series: ex) OK
setUpMockRestServer(HttpStatus.OK);
assertThat(sut.getHoge().getMessage(), is("It's 200"));
//4xx series error: ex) NOT_FOUND
setUpMockRestServer(HttpStatus.NOT_FOUND);
assertThat(sut.getHoge().getMessage(), is("It's 404"));
//5xx series error: ex) INTERNAL_SERVER_ERROR
setUpMockRestServer(HttpStatus.INTERNAL_SERVER_ERROR);
assertThat(sut.getHoge().getMessage(), is("It's 500"));
}
private void setUpMockRestServer(HttpStatus status) throws JsonProcessingException {
MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate).ignoreExpectOrder(true).build();
String response = objectMapper.writeValueAsString(new Hoge(status.value() + "That's right"));
mockServer.expect(requestTo("https://hoges/1"))
.andRespond(withStatus(status).body(response).contentType(MediaType.APPLICATION_JSON_UTF8));
}
}
--In case of client error system (4xx), HttpClientErrorException
--In case of server error system (5xx), HttpServerErrorException
--In the case of the code you set yourself, ʻUnknownHttpStatusCodeException Each of the above needs to be handled by
try-catch`
Sample.java
@Component
public class Sample {
RestTemplate restTemplate;
public Sample(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public Hoge getHoge() {
ResponseEntity<Hoge> response;
try {
response = restTemplate.getForEntity("https://hoges/1", Hoge.class);
} catch (HttpClientErrorException | HttpServerErrorException e) {
//Can be branched by statusCode
int statusCode = e.getStatusCode().value();
return new Hoge(statusCode + "That's right");
}
return response.getBody();
}
}
This can be achieved by inheriting DefaultResponseErrorHandler
and setting it to be unnecessary.
SampleResponseErrorHandler.java
@Component
public class SampleResponseErrorHandler extends DefaultResponseErrorHandler {
@Override
public void handleError(ClientHttpResponse response) throws IOException {
//By writing nothing, it does not raise an exception even if a server error and a client error occur.
}
}
Sample.java
@Component
public class Sample {
RestTemplate restTemplate;
SampleResponseErrorHandler sampleResponseErrorHandler;
public Sample(RestTemplate restTemplate, SampleResponseErrorHandler sampleResponseErrorHandler) {
this.restTemplate = restTemplate;
this.sampleResponseErrorHandler = sampleResponseErrorHandler;
}
public Hoge getHoge() {
//Set your own Defined ErrorHandler in RestTemplate
restTemplate.setErrorHandler(sampleResponseErrorHandler);
ResponseEntity<Hoge> response = restTemplate.getForEntity("https://hoges/1", Hoge.class);
//In the sample, it returns as it is, but here you can branch as much as you want with the status code that can be obtained from ResponseEntity
return response.getBody();
}
}
I wanted to get the status code and branch in detail, but I was addicted to not being able to pick it up with an exception.
Recommended Posts