我有一個休息服務,當找不到資源時發送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