Bhushan Mahajan
Bhushan Mahajan

Reputation: 171

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

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 :)

Upvotes: 1

Views: 13236

Answers (3)

Gani
Gani

Reputation: 442

Automatic conversion of Objects are neglected if you don't have getter method for the return type object. If you don't have getter method for GenericApiResponse object add one.

Upvotes: 0

Bhushan Mahajan
Bhushan Mahajan

Reputation: 171

Solution: EventSource in angular takes Content-Type : text/event-stream by default. So I created new method and added @RequestHeader(value = "Content-Type", defaultValue = "text/event-stream") as parameter.

Upvotes: 0

Vinay Prajapati
Vinay Prajapati

Reputation: 7546

From first look at your code, I can observe following problems:

  1. @ResponseBody is added but no response is returned i.e. method type is void.

  2. 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

And

ensure that request headers are set to application/json.

Also, ensrue jackson dependencies are in place.

Upvotes: 0

Related Questions