我使用Spring编写了一个REST端点。
@RequestMapping(value = "/staticdata/userPreference/extUserId/{extUserId}/type/{type}", method = RequestMethod.GET)
public ResponseEntity<List<UserPreference>> getData(
@PathVariable String extUserId,
@PathVariable String type) {
List<UserPreference> userPreferences = null;
userPreferences = staticDataService.getUserPreference(extUserId, type);
return new ResponseEntity<List<UserPreference>>(userPreferences, HttpStatus.OK);
}
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<String> handleError(HttpServletRequest req, Exception exception) {
return new ResponseEntity<String>(exception.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
当getData()方法能够成功获取数据时,它需要返回List&lt;&gt;。但是当有例外时。将返回该消息。这由handleError()方法处理。此方法返回一个String。
我的问题是: 如何使用Spring提供的restTemplate来调用这个rest服务?根据失败的成功,方法的返回可以是List或String。
答案 0 :(得分:2)
您可以尝试RestTemplate#exchange
执行请求并以ResponseEntity
获取响应,然后按List<UserPreference>
获取ResponseEntity#getBody
。
如果出现错误,您可以从HttpStatusCodeException.getResponseBodyAsString
获取错误回复。
以下是一个例子:
try {
HttpHeaders headers = new HttpHeaders();
// add some headers
HttpEntity<String> request = new HttpEntity<String>(headers);
ResponseEntity<List<UserPreference>> response = restTemplate.exchange(
YOUR_URL, HttpMethod.GET, request, new ParameterizedTypeReference<List<UserPreference>>() {});
List<UserPreference> userPerference = response.getBody();
} catch (HttpStatusCodeException e) {
String errorBody = e.getResponseBodyAsString();
}
如果您需要对错误进行更多特殊处理,可以尝试实施自定义ResponseErrorHandler
。
<强>被修改强>:
以下是演示的代码示例:
如果请求路径为&#34; / test?error = true&#34;,则RestController
有一个RequestMapping
会抛出异常。否则,返回List<String>
响应:
@RestController
public class TestController {
@RequestMapping(value = "/test", method = RequestMethod.GET)
public ResponseEntity<List<String>> getData(@RequestParam(value = "error", required = false) String error) {
if (error != null && error.equals("true")) {
throw new HttpClientErrorException(HttpStatus.NOT_FOUND);
} else {
List<String> list = new ArrayList<>();
list.add("test");
return new ResponseEntity<List<String>>(list, HttpStatus.OK);
}
}
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<String> handleError(HttpServletRequest req, Exception exception) {
return new ResponseEntity<String>("Exception Message", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
测试配置:
@SpringBootApplication
public class TestConfig {
}
有2个测试用例的集成测试。一种是测试获取List响应,另一种是测试从错误响应中获取消息体:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestConfig.class)
@WebIntegrationTest
public class IntegrationTest {
@Value("${local.server.port}")
int port;
final RestTemplate template = new RestTemplate();
@Test
public void testGetListResponse() {
String url = "http://localhost:" + port + "/test";
ResponseEntity<List<String>> response = template.exchange(
url, HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() {});
List<String> list = response.getBody();
Assert.assertEquals(list.size(), 1);
Assert.assertEquals(list.get(0), "test");
}
@Test
public void testGetErrorResponse() {
String url = "http://localhost:" + port + "/test?error=true";
String errorBody = null;
try {
ResponseEntity<List<String>> response = template.exchange(
url, HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() {
});
} catch (HttpStatusCodeException e) {
errorBody = e.getResponseBodyAsString();
}
Assert.assertEquals(errorBody, "Exception Message");
}
}
希望这可以提供帮助。