我有以下代码.
我想根据状态代码得到不同的错误消息,如果成功它应该返回对象..如何从一个方法返回这种不同的数据类型?
public EstimateTimeResult getEstimateTime(@RequestBody EstimateTimeCall estimateTimeCall,@PathVariable("empid") Long empid) throws IOException{
String URL=baseApi+"/estimates/time";
if(estimateTimeCall.getProduct_id().isEmpty() || estimateTimeCall.getProduct_id()==null || estimateTimeCall.getProduct_id()==""){
URL+="?start_latitude="+estimateTimeCall.getStart_latitude().toString()+"&start_longitude="+estimateTimeCall.getStart_longitude().toString();
}else{
URL+="?start_latitude="+estimateTimeCall.getStart_latitude().toString()+"&start_longitude="+estimateTimeCall.getStart_longitude().toString()+"&product_id="+estimateTimeCall.getProduct_id();
}
//Header Set
HttpHeaders requestHeaders=getHeader(empid);
//Do Request
HttpEntity requestEntity=new HttpEntity(requestHeaders);
ResponseEntity<EstimateTimeResult> response=restTemplate.exchange(URL,HttpMethod.GET,requestEntity,EstimateTimeResult.class);
org.springframework.http.HttpStatus statusCode=response.getStatusCode();
if(statusCode.value()==422){
return "Required start latitude,longitude";
}
return response.getBody();
}
Run Code Online (Sandbox Code Playgroud)