不使用ResponseEntity时,Spring控制器不返回值 [英] Spring controller not returning value when not using ResponseEntity

查看:64
本文介绍了不使用ResponseEntity时,Spring控制器不返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的控制器,

I have a controller like so,

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

这很好用,但是当我不使用 ResponseEntity 返回结果而是返回字符串时,它不起作用,

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";
    }

这不起作用,我收到了 404!非常感谢任何帮助

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

推荐答案

使用ReponseBody注解:

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

这篇关于不使用ResponseEntity时,Spring控制器不返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆