Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation in java spring boot

Ask Question

I am developing chat application using java springboot and Angular 7. I am using events in spring boot and angular. I am trying to generate events in spring boot for angular to listen the event. However, I am getting following error:

Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]

Here is my controller code in springboot:

@CrossOrigin("*")
@RestController
@RequestMapping("/chat")
public class MessageController {
@Autowired
MessageService messageService;
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
private static final Logger logger = LoggerFactory.getLogger(MessageController.class);
@PostMapping(consumes = "application/json", produces = "application/json")
public GenericApiResponse<Map<String, Object>>message(@RequestBody MessageRequest req) {
    logger.info("MessageController:: messagemethod [POST] /chat");
    GenericApiResponse<Map<String, Object>> responseObj = new GenericApiResponse<>();
    Object returnValue = new Object();
    try {
        returnValue = messageService.translateText(req);
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("EXCEPTION: "+e.getStackTrace().toString());
        responseObj.setStatus(Constants.ERROR);
        responseObj.setMessage("Internal Server Error");
    Map<String, Object> resMap = new HashMap<>();
    resMap.put("result", returnValue);
    resMap.put("sender", req.getSender());
    responseObj.setResponseObject(resMap);
    responseObj.setStatus(Constants.SUCCESS);
    MessageEvent messageEvent = new MessageEvent(this,"eventName", responseObj);
    applicationEventPublisher.publishEvent(messageEvent);
    return responseObj;

I am unable to figure out what is the issue and how to solve it. Please help me to solve this issue. Thanks in advance :)

curl -X POST \ http://localhost:8090/chat\ -H 'Accept: */*' \ -H 'Accept-Encoding: gzip, deflate' \ -H 'Cache-Control: no-cache' \ -H 'Connection: keep-alive' \ -H 'Content-Length: 134' \ -H 'Content-Type: application/json' \ -H 'Cookie: JSESSIONID=349102FC07B387A5D2629995D508210F' \ -H 'Host: localhost:8090' \ -H 'cache-control: no-cache' \ -d '{ "text" : "I can do this now", "sourceLanguageCode" : "en", "targetLanguageCode" : "hi", "sender" : "abc" } – Bhushan Mahajan Oct 22, 2019 at 8:25
  • @ResponseBody is added but no response is returned i.e. method type is void.

  • produces = "application/json" doesn't make sense for a void method returning no response.

  • Hence, for a rest endpoint always return some response. You can fix it by putting following as return statement in the end in your method:

    return ResponseEntity.ok("your message");
    

    Also, @ResponseBody means that response is always serialized to json hence, no need to specify , produces = "application/json" explicitly.

    Update:

    Can you please also try replacing consumes = "application/json", produces = "application/json" with

      consumes = MediaType.APPLICATION_JSON_VALUE, 
      produces = MediaType.APPLICATION_JSON_VALUE
    

    ensure that request headers are set to application/json.

    Also, ensrue jackson dependencies are in place.

    Hi, now I am returning the responseObj, so added GenericApiResponse<Map<String, Object>> as return type. And therefore I am keeping produces= "application/json" as it is. But still it is giving me same error – Bhushan Mahajan Oct 22, 2019 at 6:55 Yes, I have removed @ResponseBody as I am keeping produces= "application/json". My request body contains: sender and text. – Bhushan Mahajan Oct 22, 2019 at 7:29

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.