简体   繁体   English

不使用ResponseEntity时Spring控制器不返回值

[英]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, 效果很好,但是当我不使用ResponseEntity返回结果而是返回String时,它不起作用,

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

This does not work and I get a 404! 这不起作用,我得到了404! Any help greatly appreciated 任何帮助,不胜感激

Use ReponseBody annotation: 使用ReponseBody批注:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Junit 测试用例 Spring 启动 controller 返回 ResponseEntity 中的值 null - Junit Test case Spring boot controller returning null value in ResponseEntity 在控制器中返回错误时,Spring MVC ResponseEntity始终为空 - Spring MVC ResponseEntity is always empty when returning error in controller 在春季启动时使用ResponseEntity返回json时遇到问题 - Facing a issue when returning json with ResponseEntity in spring boot Spring ResponseEntity不返回所有对象属性 - Spring ResponseEntity not returning all object attributes Spring Boot - 使用 Lombok 时,Rest Controller 返回空对象 - Spring Boot - Rest Controller is returning empty object when using Lombok Spring 响应实体 - Spring ResponseEntity 当 RestApi 有 ResponseEntity 时<void> spring 有返回值,它返回 ClientProtocolException - When the RestApi has ResponseEntity<void> of spring has return value it returns ClientProtocolException Spring - HttpMessageConversionException 使用带有 Map 的 ResponseEntity<string,jsonobject></string,jsonobject> - Spring - HttpMessageConversionException using ResponseEntity with Map<String,JSONobject> Spring:使用 ResponseEntity 返回空的 HTTP 响应<Void>不起作用 - Spring: Returning empty HTTP Responses with ResponseEntity<Void> doesn't work spring控制器返回带有post的404和带有get的文件以生成响应性 - spring controller returns 404 with post and file with get to generate responseentity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM