2
I am trying to return a automatically created key in a spring-boot service (this one, by the way).
I tried to directly return an integer by end-point:
@RestController
public class Controller {
Random r = new Random();
@RequestMapping(path = "/teste", method = RequestMethod.GET, produces = "text/plain")
public int teste() throws SQLException {
return r.nextInt();
}
@RequestMapping(path = "/teste2", method = RequestMethod.GET, produces = "text/plain")
public String teste2() throws SQLException {
return "" + r.nextInt();
}
}
However, when trying to access http://localhost/teste
, get error 406:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Apr 23 10:32:15 GFT 2019
There was an unexpected error (type=Not Acceptable, status=406).
Could not find acceptable representation
And no error appears in the console. But when converting to string, it appeared without error.
However, access http://localhost/teste2
returns the correct number.
How do I return an integer directly, without having to convert to string, in spring-boot?
You want to return only the value (regardless of type) or you need to be an int?
– Filipe L. Constante
@Constant in my ideal world and in my specific case would only be the
int
. At worst I have the option to turn into string, but I would not like to do this explicitly– Jefferson Quesado
If you have a generic alternative you are also welcome
– Jefferson Quesado