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

时间:2015-09-16 14:25:57

标签: spring-mvc post request response swagger

我正在使用Spring-MVC并且我正在尝试在POST请求之后发回一个响应主体。看起来Spring-MVC正在发回一些东西,但它是空的..

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

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

提前致谢

代码

  @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;

  }

Application是由swagger-codegen生成的类。如果我将它与GET方法一起使用,它只返回一个JSON对象..

打印dummyResponse

如果我打印变量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]}>

2 个答案:

答案 0 :(得分:0)

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

您不必添加任何特定标头。

答案 1 :(得分:0)

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

  • “访问控制允许来源”
  • “接入控制允许凭证”

由于我也是手动添加它们,这两个标题会出现两次。因此,我只需删除它们就可以了。