I have a requirement that needs to the controller be able to receive both JSON and XML.
I have put the produces
and consumes
tags in the RequestMapping but it is not working. When i do a POST to the endpoint using JSON as the body, it work fine.
When i do a POST using a XML as the body the HTTP message 415 Unsupported Media Type
is thrown.
@RestController
@AllArgsConstructor
@NoArgsConstructor
@RequestMapping(value = "/calendarios-publicacao",
produces = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE },
consumes = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE } )
public class CalendarioPublicacaoController implements ControllerFacade<CalendarioPublicacaoDto, CalendarioPublicacaoDto> {
@Autowired
private CalendarioPublicacaoService calendarioPublicacaoService;
@Override
@PostMapping
@ApiResponses({
@ApiResponse(code = 201, message = "Calendário de publicação criado com sucesso."),
@ApiResponse(code = 400, message = "Dados enviados incorretamente."),
@ApiResponse(code = 403, message = "Sem permissões para criar calendários de publicação."),
@ApiResponse(code = 409, message = "Já existe um calendário para este módulo.")
})
public ResponseEntity<CalendarioPublicacaoDto> create(@RequestBody CalendarioPublicacaoDto object, HttpServletRequest request) {
return ResponseEntity.created(URI.create(request.getRequestURL().append("/").append(calendarioPublicacaoService.create(object).getId()).toString())).build();
}
}
Example of the request with curl:
I receive 415.
curl -v -XPOST -d '<?xml version="1.0" encoding="UTF-8"?>
<CalendarioPublicacaoDto>
<anoReferencia>0</anoReferencia>
<dataSensibilizacao>2022-02-09T18:56:36.525Z</dataSensibilizacao>
<entidade>SESI</entidade>
<modulo>
<id>4</id>
</modulo>
<periodicidade>AUTOMATICO</periodicidade>
<periodoReferencia>0</periodoReferencia>
</CalendarioPublicacaoDto>' 'localhost:9090/calendarios-publicacao'
And via Insomnia the same 415 error.
Should I create an configuration class?
SOLVED
I only needed to add the @XmlRootElement(name="") annotation in my Dto.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.