Spring-MVC:在 POST 方法后发送响应

Spring-MVC: Send a Response after a POST method

我正在使用 Spring-MVC,我正在尝试在 POST 请求后发回响应 body。似乎 Spring-MVC 正在发送一些东西返回但它是空的..

第一个问题:是否可以在 POST 请求后发送 json 响应?

第二个问题:如果是,我是否必须包括一些特定的 headers?

提前致谢

代码

  @CrossOrigin
  @ApiOperation(value = "Update an application", notes = "Update a document given its id. If a field is let empty, the value will not be updated.", response = Application.class)
  @ApiResponses(value = {
    @ApiResponse(code = 200, message = "successful operation"),
    @ApiResponse(code = 403, message = "Request could not be authorized."),
    @ApiResponse(code = 500, message = "Internal Server Error."),
    @ApiResponse(code = 400, message = "Malformed request.") })
  @RequestMapping(value = "/update",
    produces = { "application/json" },
    consumes = { "application/json", "text/json" },
    method = RequestMethod.POST)
  public ResponseEntity<Application> updateApplication(@ApiParam(value = "ID of the document", required = true) @RequestParam(value = "docId", required = true) String docId,
@RequestBody @ApiParam(value = "Additional data given to the application"  ) ApplicationData data)
      throws NotFoundException {

        ApplicationControllerMock dummyClass = new ApplicationControllerMock();

        ResponseEntity<Application> dummyResponse = dummyClass.updateApplication(docId, data);

        return dummyResponse;

  }

class Application 是由 swagger-codegen 生成的 class.. 如果我用 GET 方法使用它,它只是 returns 我一个JSON object..

打印虚拟响应

如果我打印变量 dummyResponse,我得到这个:

<200 OK,class Application {
  docId: cdb226df5398f564173db460b52f40de
  companyId: 123
  projectId: 123
  rev: 2-fce2134de32fae95cee4192debef4600
  docType: 3
  docCreated: 2015-09-01T02:30:14Z
  docDeleted: false
  data: class ApplicationData {
  status: string
  domain: string
  name: string
  version: string
  gaCode: string
}

  id: a2cf532b721eedb542e884820b180b55
}
,{Access-Control-Allow-Credentials=[true], Access-Control-Allow-Origin=[*], Access-Control-Allow-Methods=[GET, OPTIONS, POST, HEAD], Access-Control-Allow-Headers=[Origin, X-Requested-With, Content-Type, Accept], Content-Type=[application/json], Access-Control-Max-Age=[3600]}>

是的,当请求是 POST 时可以发回响应。

您不必添加任何特定的 headers。

我发现了问题.. @CrossOrigin 自动创建了这两个 headers:

  • "Access-Control-Allow-Origin"
  • "Access-Control-Allow-Credentials"

由于我也是手动添加的,所以这两个 headers 出现了两次..因此,我只需要删除它们就可以了..