Spring RestTemplate invoking webservice with errors and analyze status code

前端 未结 5 916
执笔经年
执笔经年 2020-12-07 17:39

I designed a webservice to perform a task if request parameters are OK, or return 401 Unauthorized HTTP status code if request parameters are wrong or empty.

I\'m us

相关标签:
5条回答
  • 2020-12-07 18:27

    As an alternative to the solution presented by nilesh, you could also use spring class DefaultResponseErrorHandler. You also need to ovveride its hasError(HttpStatus) method so it does not throw exception on non-successful result.

    restTemplate.setErrorHandler(new DefaultResponseErrorHandler(){
        protected boolean hasError(HttpStatus statusCode) {
            return false;
        }});
    
    0 讨论(0)
  • 2020-12-07 18:31

    You can use spring-test. It's much easier:

    @WebAppConfiguration
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:your-context.xml")
    public class BasicControllerTest {
    
            @Autowired
            protected WebApplicationContext wac;
            protected MockMvc mockMvc;
    
            @Before
            public void setUp() throws Exception {
            mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
            }
    
            @Test
            public void testUnauthorized(){
    
            mockMvc.perform(MockMvcRequestBuilders
                                .post("your_url")
                                .param("name", "values")
            .andDo(MockMvcResultHandlers.print())
            .andExpect(MockMvcResultMatchers.status().isUnauthorized()
            .andExpect(MockMvcResultMatchers.content().string(Matchers.notNullValue()));
            }
    }
    
    0 讨论(0)
  • 2020-12-07 18:34

    You need to implement ResponseErrorHandler in order to intercept response code, body, and header when you get non-2xx response codes from the service using rest template. Copy all the information you need, attach it to your custom exception and throw it so that you can catch it in your test.

    public class CustomResponseErrorHandler implements ResponseErrorHandler {
    
        private ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler();
    
        public boolean hasError(ClientHttpResponse response) throws IOException {
            return errorHandler.hasError(response);
        }
    
        public void handleError(ClientHttpResponse response) throws IOException {
            String theString = IOUtils.toString(response.getBody());
            CustomException exception = new CustomException();
            Map<String, Object> properties = new HashMap<String, Object>();
            properties.put("code", response.getStatusCode().toString());
            properties.put("body", theString);
            properties.put("header", response.getHeaders());
            exception.setProperties(properties);
            throw exception;
        }
    }
    

    Now what you need to do in your test is, set this ResponseErrorHandler in RestTemplate like,

    RestTemplate restclient = new RestTemplate();
    restclient.setErrorHandler(new CustomResponseErrorHandler());
    try {
        POJO pojo = restclient.getForObject(url, POJO.class); 
    } catch (CustomException e) {
        Assert.isTrue(e.getProperties().get("body")
                        .equals("bad response"));
        Assert.isTrue(e.getProperties().get("code").equals("400"));
        Assert.isTrue(((HttpHeaders) e.getProperties().get("header"))
                        .get("fancyheader").toString().equals("[nilesh]"));
    }
    
    0 讨论(0)
  • 2020-12-07 18:42

    In my rest service I catch HttpStatusCodeException instead of Exception since HttpStatusCodeException has a method for getting the status code

    catch(HttpStatusCodeException e) {
        log.debug("Status Code", e.getStatusCode());
    }
    
    0 讨论(0)
  • 2020-12-07 18:46

    Since Spring 4.3, There is a RestClientResponseException which contain actual HTTP response data, such as status code, response body and headers. And you can catch it.

    RestClientResponseException Java Doc

    0 讨论(0)
提交回复
热议问题