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.

Content-type: application/json is not autogenerated for @RestController return type?

See original GitHub issue

Example:

@Tag(name = "deployments")
@RestController
public class DeploymentController {
    @PostMapping("/bla")
    @ResponseStatus(HttpStatus.ACCEPTED)
    @Operation(operationId = "deployment")
    public DeploymentPostResponse createDeployment(@Valid @RequestBody DeploymentPostRequest deploymentPostRequest) {
return new DeploymentPostRequest();
}
}

Generates (note missing content-type: application/json, request however has it)

{
  "responses": {
    "202": {
      "description": "Accepted",
      "content": {
        "*/*": {
          "schema": {
            "$ref": "#/components/schemas/DeploymentPostResponse"
          }
        }
      }
    }
  }
}

Then after adding @Opeation with application/json on createDeployment

@Operation(operationId = "deployment", responses=@ApiResponse(content = @Content(mediaType = "application/json")))

I get:

{
  "responses": {
    "default": {
      "description": "default response",
      "content": {
        "application/json": {}
      }
    }
  }
}

Few things which seems unclear:

  1. Why content-type generated for say for request ? but not for response?
  2. Would it make sense to combine autogen with annotations, annotations overriding? Or that would be too verbose?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
bnasslahsencommented, May 11, 2020

Here is the explanation:

springdoc.default-consumes-media-type default value is: application/json And springdoc.default-produces-media-type default value is: */*

You can view more details here:

You can change these default values if you need.

The most important thing, is that once you fill the mediaType in the @Operation annotation it gets used.

You can also fill the mediaType (produces and consumes) on the level of your @PostMapping which makes more sense (so you won’t need to fill it on @Operation` annotation)

0reactions
bnasslahsencommented, May 11, 2020

You can define HTTP 400 errors in controllerAdvice, so it can be used. So it can be half automatic (202) and half manual with HTTP (400) responses.

If its specific to one controller. Once you add responses on the controller level, it will be used as reference. So it can’t be half manual and and half automatic At this moment, you should fill the expected responses codes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Set JSON Content Type In Spring MVC - Baeldung
Let's start with a simple example of an API exposing a JSON string. Based on the content type present in the header, @ResponseBody...
Read more >
"Content type 'application/json;charset=UTF-8' not supported ...
I was able to solve it by removing @JsonManagedReference.
Read more >
22. Web MVC framework - Spring
The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler ...
Read more >
Spring Restful Web Services Example with JSON, Jackson ...
Create Employee POST Rest Call: Make sure request Content-Type is set to “application/json” otherwise you will get HTTP Error Code 415. Sping- ...
Read more >
What is Spring MVC: @Controllers & @RestControllers
It needs to take whatever the output from your @Controller was and convert it back to HTML/JSON/XML . DispatcherServlet Overview. The whole ...
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