Reputation: 149
I have Controller :-
@RequestMapping(value = "/360ScoreCard", method = RequestMethod.POST)
public @ResponseBody Object get360ScoreCard(@RequestParam("userEmail") String userEmail,
@RequestParam("valid_from") String from, @RequestParam("valid_till") String till) throws ParseException {
if (userEmail != null) {
String uri = util.getRestApiHost() + "userScoreFor360" + "?userEmail=" + userEmail;
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date valid_from = (Date) formatter.parse(from);
Date valid_till = (Date) formatter.parse(till);
MultiValueMap<String, Date> paramMap = new LinkedMultiValueMap<String, Date>();
paramMap.add("valid_from", valid_from);
paramMap.add("valid_till", valid_till);
RestTemplate restTemplate = new RestTemplate();
Response response = restTemplate.postForObject(uri, paramMap, Response.class);
return response;
}
return "User Email is Null";
}
I have API:-
@RequestMapping(value = "/userScoreFor360", method = RequestMethod.POST)
public Object getScoreFor360( @RequestParam String userEmail,Date valid_from, Date valid_till, HttpServletResponse initResponse) {
logger.debug("Enter into method getScoreFor360()");
Response response=new Response();
JSONObject obj = new JSONObject();
try {
response = scoreService.get360Score(userEmail,valid_from,valid_till);
Feedback360 fbThreeSixty = (Feedback360) response.getResult();
List<ThreeSixtyRatings> listOfRating = fbThreeSixty.getRating_list();
JSONArray jsonArray = new JSONArray();
for (ThreeSixtyRatings ratings : listOfRating) {
JSONObject json = new JSONObject();
json.put("category", ratings.getCategory());
json.put("rating_average", ratings.getRating_average());
json.put("number_feedbacks", ratings.getNumber_feedbacks());
jsonArray.put(json);
}
obj.put("rating_list", jsonArray);
obj.put("aggregate", fbThreeSixty.getAggregate());
} catch (Exception e) {
logger.error("Exception in userScoreFor360 Mobile Api :", e);
}
logger.debug("Exit from method getScoreFor360()" + new Gson().toJson(response));
return obj.toString();
}
I want to pass Date from Controller to API. Whenever i am trying to pass the Date from controller to API, it is throwing exception:-
2019-06-24 15:15:34.437 WARN 2672 --- [nio-8180-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '1516300200000'; nested exception is java.lang.IllegalArgumentException]
I am not able to understand that even after adding the Dates to MultivalueMap where it is of Date type, how it is API is treating Date as String type?
Please Note:- I cannot change the API.
Upvotes: 0
Views: 2846
Reputation: 1528
You have missed @RequestParam
, but also if you need to consume as a Date
you can do
@RequestMapping(value = "/userScoreFor360", method = RequestMethod.POST)
public Object getScoreFor360( @RequestParam String userEmail,
@RequestParam @DateTimeFormat(pattern="yyyy-MM-dd") Date valid_from,
@RequestParam @DateTimeFormat(pattern="yyyy-MM-dd") Date valid_till,
HttpServletResponse initResponse) {
//LOGIC
return obj.toString();
}
or only way to consume valid_from and valid_to as String
and format it to date using SimpleDateFormat
.
Upvotes: 2