Spring controller not returning value when not usi

2019-08-03 04:01发布

问题:

I have a controller like so,

@RequestMapping(value = "/sample")
    public ResponseEntity<> search() throws Exception{
        return new ResponseEntity("Hello World",OK);
    }

This works fine, but when I dont use ResponseEntity for returning the result but return the String instead, it does not work,

@RequestMapping(value = "/sample")
public String search() throws Exception{
    return "Hello Worls";
    }

This does not work and I get a 404! Any help greatly appreciated

回答1:

Use ReponseBody annotation:

@RequestMapping(value = "/sample")
public @ResponseBody String search() throws Exception{
    return "Hello Worls";
}