【问题标题】:Spring : Responding to a REST-ful API callSpring:响应 REST-ful API 调用
【发布时间】:2014-08-24 09:31:17
【问题描述】:

我正在尝试在您的应用程序中实现 REST API 端点。

@Controller
 public class HerokuController {

    @RequestMapping(value = "/heroku/resources/", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public  ResponseEntity<JSONObject> provision(@RequestBody JSONObject body){
        System.out.println("called! \n");
        JSONObject response = new JSONObject();
        response.put("id", 555);
        response.put("message", "Provision successful!");
          return new ResponseEntity<JSONObject>(response,HttpStatus.OK);        
    }

所以我编写了这个类,其中包含一个映射方法(heroku/ressources)。 但是当我尝试调用它时,我收到 404 错误,因为找不到 /WEB-INF/heroku/resources.jsp。但是,我什至不想获得视图,而是获得 HTTP 响应。

谁能告诉我我们一般应该修改哪个配置文件来告诉 Spring 这个控制器不想发回视图而是 HTTP 响应?

但是,如果我将其更改为此方法,则会调用该方法:

@RequestMapping(value = "/heroku/resources/", method = RequestMethod.POST)
    public  ModelAndView provision(final HttpServletRequest request){
            System.out.println("called! \n");
            JSONObject response = new JSONObject();
            response.put("id", 555);
            response.put("message", "Provision successful!");
            final Map<String, Object> result = new HashMap<String, Object>();
          return new ModelAndView("jsonView",result);   
    }

所以将返回类型改为"ModelAndView"

谢谢。

【问题讨论】:

  • @Genzotto,我认为这不会回答 OP 的问题。我相信他需要做的是添加@ResponseBody 注释。问题是如果你不告诉它,Spring MVC 会尝试“猜测”你的意思。它通过查看请求 URI、挑选最后一位(在您的情况下为“资源”)并添加“.jsp”的后缀来完成此操作。在这种情况下,OP 不希望返回视图的名称,而是返回 view 的内容。

标签: java spring rest jakarta-ee spring-mvc


【解决方案1】:

您缺少@ResponseBody

@RequestMapping(value = "/heroku/resources/", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity<JSONObject> provision(@RequestBody JSONObject body){
    System.out.println("called! \n");
    JSONObject response = new JSONObject();
    response.put("id", 555);
    response.put("message", "Provision successful!");
      return new ResponseEntity<JSONObject>(response,HttpStatus.OK);        
}

【讨论】:

  • youtube.com/watch?v=wylViAqNiRA 在本教程中,它说当我们使用“ResponseEntity”Objet 时,我们不使用@ResponseBody 注释。
  • 他们在这里(春季文档)使用它:docs.spring.io/spring-roo/reference/html/base-json.html
  • @user3242743 描述一下您所说的“没用”是什么意思。我可以证明,使用@ResponseBody 绝对是通知 Spring 您打算返回不是“视图”的内容的方式。
  • 我的意思是我得到了一个:在 DispatcherServlet 中找不到带有 URI 的 HTTP 请求的映射,名称为“appServlet”。
【解决方案2】:

我曾经遇到过同样的问题,为了解决你可以使用@RestController 而不是@controller(这将默认发送Json)并且你可以像这样定义你的方法

@RequestMapping(value = "/heroku/resources/", method = RequestMethod.POST)
public JsonOut provision(@RequestBody JsonIn json)

我总是用我将从客户端获得的值来制作我的对象,并且总是输出的定义

public class JsonOut{

    protected String id;
    protected String message;

    ...set ....get

}

你必须在spring xml文件中放入这两个值

<mvc:annotation-driven />

<context:annotation-config/> 

使用此配置,您将始终拥有 json!

这适用于 spring 4,我不知道 spring 3 是否可以工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    • 1970-01-01
    • 2022-07-24
    • 1970-01-01
    • 2014-08-29
    • 2020-03-15
    • 1970-01-01
    相关资源
    最近更新 更多