Content-type: application/json is not autogenerated for @RestController return type?
See original GitHub issueExample:
@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:
- Why content-type generated for say for request ? but not for response?
- Would it make sense to combine autogen with annotations, annotations overriding? Or that would be too verbose?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Here is the explanation:
springdoc.default-consumes-media-type
default value is:application/json
Andspringdoc.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)
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.