我有一个休息服务,当找不到资源时发送404错误。 这里我控制器的来源和发送异常HTTP 404。Spring MVC - 发生http 404时RestTemplate启动异常
@Controller
@RequestMapping("/site")
public class SiteController
{
@Autowired
private IStoreManager storeManager;
@RequestMapping(value = "/stores/{pkStore}", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public StoreDto getStoreByPk(@PathVariable long pkStore) {
Store s = storeManager.getStore(pkStore);
if (null == s) {
throw new ResourceNotFoundException("no store with pkStore : " + pkStore);
}
return StoreDto.entityToDto(s);
}
}
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException
{
private static final long serialVersionUID = -6252766749487342137L;
public ResourceNotFoundException(String message) {
super(message);
}
}
当我尝试使用此代码RestTemplate称呼它:
ResponseEntity<StoreDto> r = restTemplate.getForEntity(url, StoreDto.class, m);
System.out.println(r.getStatusCode());
System.out.println(r.getBody());
我收到此异常:
org.springframework.web.client.RestTemplate handleResponseError
ATTENTION: GET request for "http://........./stores/99" resulted in 404 (Introuvable); invoking error handler
org.springframework.web.client.HttpClientErrorException: 404 Introuvable
我在想我可以探索我的responseEntity对象,并用statusCode做一些事情。但例外是启动和我的应用程序下降。
是否有restTemplate不发送异常但填充我的ResponseEntity的特定配置。
非常感谢您的帮助。
-
卢瓦克
能够通过浏览器来访问其他服务?或者那还扔404? – Akshay 2013-04-24 15:18:00
你有没有找到答案? – leojh 2014-02-04 20:22:11