How to return an integer when accessing an end-point?

Asked

Viewed 104 times

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?

  • @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

  • If you have a generic alternative you are also welcome

2 answers

1

Probably your Accept request header asks for a JSON and your end-point is not returning this, so status 406. The string answer works because JSON is essentially a string.

In the case of the integer, the conversion of the response to JSON must be done explicitly, because the conversion is only done implicitly when the annotation @ResponseBody is used.

  • I will check it, but in my memory the browser accepted text/plain. Do for curl the requisition to have full control of the headers

  • I tried using the note @ResponseBody but I did not receive a satisfactory reply.

  • 1

    @Jeffersonquesado I did not pay attention to @RestController who already adds @ResponseBody implicitly

  • I tried only with curl http://localhost/teste -i, and kept getting 406. The headers which are added by Curl itself are: host: localhost user-agent: curl/7.60.0 accept: */*

0

Try it like this:

    Random r = new Random();

@RequestMapping(path = "/teste", method = RequestMethod.GET, produces = "text/plain")
public ResponseEntity<String> teste() {

    Integer numero = r.nextInt();

    return new ResponseEntity<String>(numero.toString(), HttpStatus.OK);
}

Well, I tested it this way and it worked.

  • So I’m turning into string explicitly, which I wanted to escape...

  • I’ve been through this in another project, I’ll see if I can find it for you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.