question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

springmvc: @ResponseStatus annotation for CREATED results in doubled response description

See original GitHub issue

Generated result:

...
"responses" : {
  "200" : {
    "description" : "successful operation",
      "schema" : {
      "$ref" : "#/definitions/ResultDto"
    }
  },
  "201" : {
    "description" : ""
  }
}
...

Expected result:

...
"responses" : {
  "201" : {
    "description" : "successful operation",
      "schema" : {
      "$ref" : "#/definitions/ResultDto"
    }
  }
}
...

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
a-n710commented, May 6, 2019
    @ResponseStatus(code = HttpStatus.ACCEPTED)
    public ResponseEntity<String> test1()
...
    @ResponseStatus(code = HttpStatus.ACCEPTED)
    public String test2()

from my perspective both of these methods should result in

        202:
          description: "successful operation"
          schema:
            type: "string"

At the same time, both of these

    @ApiResponses({@ApiResponse(code = 201, message = "201 message"), @ApiResponse(code = 409, message = "conflict message")})
    public String test4()
...
    @ResponseStatus(code = HttpStatus.CREATED)
    @ApiResponses({@ApiResponse(code = 201, message = "201 message"), @ApiResponse(code = 409, message = "conflict message")})
    public String test5()

should result in

        201:
          description: "201 message"
          schema:
            type: "string"
        409:
          description: "conflict message"

and finally,

    @ResponseStatus(code = HttpStatus.OK)
    @ApiResponses({@ApiResponse(code = 201, message = "201 message"), @ApiResponse(code = 409, message = "conflict message")})
    public String test6()

should also be converted into

        201:
          description: "201 message"
          schema:
            type: "string"
        409:
          description: "conflict message"

The last one is a little controversial and can be expected to become

        200:
          description: "successful operation"
          schema:
            type: "string"
        201:
          description: "201 message"
        409:
          description: "conflict message"

but I think that if someone is explicitly specifying api responses there is no reason to avoid defining all of them, including the 200 one, if needed. Currently you PR fails to specify type: string in all cases and duplicates status code in the test1-test2 block .

0reactions
bratwurztcommented, May 6, 2019

As far as I can see, if we merge our PRs, result should satisfy both parties: if ApiResponses is specified, we ignore ResponseStatus and assume that definition in ApiResponses is complete.

I don’t think so - the whole point of this PR is to remove/replace default 200 response (which is added in com.github.kongchen.swagger.docgen.reader.SpringMvcApiReader#parseMethod) in special cases where you don’t want 200 at all. For example - combination of ResponseStatus(201) and ApiResponse(204) is perfectly legitimate: 201 is default, 204 is a special case. I don’t think we should override scheme or description in case of 204 with the one from 200 - no content has no content. Also the case which you pointed out at the end - if you set any ApiResponses and have set default on ResponseStatus, it will be ignored.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Spring @ResponseStatus to Set HTTP Status Code
In this short tutorial, we will see the most straightforward way: using the @ResponseStatus annotation. 2. On Controller Methods.
Read more >
Exception Handling in Spring MVC
When an annotated exception is thrown from a controller method, and not handled elsewhere, it will automatically cause the appropriate HTTP ...
Read more >
spring 4.2 application event is firing twice with Spring MVC ...
I suspect indeed that the EventListener bean is registered twice or something. You can enable org. springframework. context.
Read more >
Spring Boot @ResponseStatus Annotation - Java Guides
@ResponseStatus marks a method or exception class with the status code and reason message that should be returned. The status code is applied...
Read more >
Logging Requests and Responses in Spring (including body)
Recently we have found some problems trying to log a complete Request and Response in a Spring Application. When we talk about a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found