简体   繁体   中英

Spring controller not returning value when not using ResponseEntity

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

Use ReponseBody annotation:

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM