Skip to content

ControllerAdvice content type defaults to */* #442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bfs-natewallman opened this issue Feb 18, 2020 · 5 comments
Closed

ControllerAdvice content type defaults to */* #442

bfs-natewallman opened this issue Feb 18, 2020 · 5 comments
Labels
enhancement New feature or request

Comments

@bfs-natewallman
Copy link

bfs-natewallman commented Feb 18, 2020

When building a class annotated with '@ControllerAdvice' to handle exception conversion to JSON the default content type is '/' which is not ideal. At the very least we should be able to override this at the class level with an annotation that overrides the behavior.

Steps to reproduce the behavior:

  • What version of spring-boot you are using?
    2.2.2.RELEASE

  • What modules and versions of springdoc-openapi are you using?
    name: 'springdoc-openapi-ui', version: '1.2.31'
    name: 'springdoc-openapi-security', version: '1.2.31'

  • What is the actual and the expected result using OpenAPI Description (yml or json)?
    Actual something like: "content":{"/"..."
    Expected something like: "content":{"application/json"..."

I have actually found a work to annotate your methods with:

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(value = Exception.class) @RequestMapping(method = { GET, POST, PUT, DELETE, PATCH }, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<ExceptionReportViewModel> handleException(HttpServletRequest req, Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); }

I don't like having to apply all the methods to the RequestMapping annotation as that's not recommended. I would like to be able to apply a RequestMapping at the class level and have that be used like such:

@ControllerAdvice @RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE) public class UseJsonExceptionResponseControllerAdvice {

This doesn't work unfortunately. Is there another way this should be done or is this an enhancement to be made?

@bnasslahsen
Copy link
Collaborator

@bfs-natewallman,

With v1.2.32, you will be able to override the default produces mediaType:
You can use the following property:

springdoc.default-produces-media-type=application/json

@bfs-natewallman
Copy link
Author

@bnasslahsen Two questions regarding this fix:
1.) I think an improvement to this fix is to actually honor the @RequestMapping annotation if one is applied. This gives greater flexability.
2.) In the absense of #1, how can I configure those properties in code as opposed to via a properties file?

@bnasslahsen
Copy link
Collaborator

For 1), its already based on @RequestMapping annotation. But if you have better improvements, you can propose a PR.

For 2), you have access to SpringDocConfigProperties Bean, so you can have full control:

@Bean
public BeanFactoryPostProcessor beanFactoryPostProcessor1(SpringDocConfigProperties springDocConfigProperties) {
	return beanFactory -> springDocConfigProperties.setDefaultProducesMediaType(MediaType.APPLICATION_JSON_VALUE);
}

@swarup-bhol
Copy link

Cannot solve the exception for with preset Content-Type 'video/mp4' .While playing video

@bnasslahsen
Copy link
Collaborator

@swarup-bhol,

If you are reporting a bug, please help to speed up problem diagnosis by providing as
much information as possible:

  • You need to describe your context (the title of an issue is not enough)
  • What version of spring-boot you are using?
  • What modules and versions of springdoc-openapi are you using?
  • What are the actual and the expected result using OpenAPI Description (yml or json)?
  • Provide with a sample code (HelloController) or Test that reproduces the problem

Please make sure you have read the section Using GitHub Issues in the contribution guide.

@springdoc springdoc deleted a comment from swarup-bhol Aug 7, 2020
@bnasslahsen bnasslahsen added the enhancement New feature or request label Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants