Spring boot not able to parse custom ServiceResponse java class
I am developing spring boot microservice project. I need to fetch get() response from one microserice to another microservice using resttemplate. But I am not able to parse the ServiceResponse object.
----------
Below is the code of ServiceResponse object in User Microservice:
public class ServiceResponse {
private HttpStatus status;
private T response;
public ServiceResponse() {
}
public ServiceResponse(HttpStatus status, T response) {
super();
this.status = status;
this.response = response;
}
public HttpStatus getStatus() {
return status;
}
public void setStatus(HttpStatus status) {
this.status = status;
}
public T getResponse() {
return response;
}
public void setResponse(T response) {
this.response = response;
}
@Override
public String toString() {
return "ServiceResponse [status=" + status + ", response=" + response + "]";
}
} ---------- Below is code in UserController
@GetMapping("/search/{userId}")
public ServiceResponse findUser(@PathVariable Integer userId)
{
User getUser = userService.getUser(userId);
return new ServiceResponse<>(HttpStatus.OK,getUser);
}
------------------
Below is the code of entity User.java
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String email;
private String paymentMethod;
private String srcAccount;
private double availableAmount;
public User() {
}
public User(int id, String name, String email, String paymentMethod, String srcAccount, double availableAmount) {
this.id = id;
this.name = name;
this.email = email;
this.paymentMethod = paymentMethod;
this.srcAccount = srcAccount;
this.availableAmount = availableAmount;
}
}
===============
Below code in the Order Service...used to fetch data from User Service
String userURL = "http://localhost:9190/users/search/"+order.getUserId();
ServiceResponse sr = restTemplate.getForObject(userURL, ServiceResponse.class);
User testuser = (User) sr.getResponse(); //this line causing error.
But not able to parse the object.
Getting error as :
ERROR 26424 --- [nio-9191-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class com.project.order_service.dto.User (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; com.project.order_service.dto.User is in unnamed module of loader 'app')] with root cause
java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class com.project.order_service.dto.User (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; com.project.order_service.dto.User is in unnamed module of loader 'app')
===============