I'm making a client-server application that sends a matrix to a server, where its determinant is computed and then sent back to the client. I've made this wrapper class:
public class MatrixDTO { // with getters and setters
private double[][] matrix;
private double determinant;
}
And I've also implemented the server logic for obtaining the determinant from the MatrixDTO object. I've added this RestController in the server:
@RestController
public class MatrixController {
@RequestMapping(value = "/", method = RequestMethod.POST)
public MatrixDTO postMapping(@RequestParam MatrixDTO matrixDTO) {
// code to compute determinant ommitted
matrixDTO.setDeterminant(determinant);
return matrixDTO;
}
Then in the client I've added this method of sending the request:
final String uri = "http://localhost:8080/?matrixDTO={matrixDTOparam}";
// initialized wrapper object only with matrix data
MatrixDTO input = new MatrixDTO(data);
Map<String, MatrixDTO> params = new HashMap<>();
params.put("matrixDTOparam", input);
RestTemplate restTemplate = new RestTemplate();
result = restTemplate.postForObject(uri, input, MatrixDTO.class, params);
// now I should be able to extract the determinant with result.getDeterminant()
Many hours were lost trying to get this simple code to work. The error is:
Failed to convert value of type 'java.lang.String' to required type 'MatrixDTO';
nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'MatrixDTO':
no matching editors or conversion strategy found]
My question is the following: should I choose another approach for my problem, and if not, is there an easy way to make the code work? I'm looking for a simple implementation and not a lot of configuration to be done. Thanks.
So far, my error seems to be using @RequestParam instead of @RequestBody in the controller. By changing that and using this code in the client:
final String uri = "http://localhost:8080/";
MatrixDTO input = new MatrixDTO(data);
RestTemplate restTemplate = new RestTemplate();
result = restTemplate.postForObject(uri, input, MatrixDTO.class);
it appears to work well enough.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.