Tengo un problema para enviar datos JSON al método de RestController. Recibí este tipo de error que se muestra a continuación.
error: "Unsupported Media Type" message: "Content type 'application/json;charset=UTF-8' not supported"Aquí está mi método relacionado en js.
function postQuestion() { url = contextPath + "post_question/" + productId; question = $("#question").val(); jsonData = {questionContent: question}; $.ajax({ type: 'POST', url: url, beforeSend: function(xhr) { xhr.setRequestHeader(csrfHeaderName, csrfValue); }, data: JSON.stringify(jsonData), contentType: 'application/json' }).done(function(response) { $("#modalDialog").on("hide.bs.modal", function(e) { $("#question").val(""); }); ..... }).fail(function() { ..... }); }Aquí está mi método definido en RestController.
@PostMapping(value="/post_question/{productId}") public ResponseEntity<?> postQuestion(@RequestBody Question question, @PathVariable(name = "productId") Integer productId, HttpServletRequest request) throws ProductNotFoundException, IOException { LOGGER.info("QuestionRestController | postQuestion is called"); LOGGER.info("QuestionRestController | postQuestion | question : " + question.getQuestionContent()); LOGGER.info("QuestionRestController | postQuestion | productId : " + productId); Customer customerUser = authenticationControllerHelperUtil.getAuthenticatedCustomer(request); LOGGER.info("QuestionRestController | postQuestion | customerUser : " + customerUser.getFullName()); if (customerUser == null) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } questionService.saveNewQuestion(question, customerUser, productId); return new ResponseEntity<>(HttpStatus.OK); }Aquí está mi entidad de pregunta que se muestra a continuación.
@Entity @Table(name = "questions") @Getter @Setter public class Question implements Serializable{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(name = "question") private String questionContent; }Incluso si definí dataType:'json' en js o consumes={"application/json;charset=UTF-8"} en el método de RestController, todo esto no pudo ayudarme a solucionar mi problema.
¿Cómo puedo arreglarlo?