I am using springboot and rabbitmq to queue requests which will then be processed later by the receiver. The MQ sender works perfectly when we send any object, however it is throwing the below exception when a HTTP request is being tried to get queued.
org.springframework.amqp.support.converter.MessageConversionException: Failed to convert Message content
at org.springframework.amqp.support.converter.AbstractJackson2MessageConverter.createMessage(AbstractJackson2MessageConverter.java:463)
at org.springframework.amqp.support.converter.AbstractMessageConverter.toMessage(AbstractMessageConverter.java:70)
at org.springframework.amqp.support.converter.AbstractMessageConverter.toMessage(AbstractMessageConverter.java:58)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertMessageIfNecessary(RabbitTemplate.java:1831)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:1137)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:1130)
Conversion exception occurs while writing HTTP request as JSON to the MQ.
How can we push a HTTP request as such to the MQ, so that I can use it to identify tenant/security/headers while processing the request from the queue.
The MQ Config, exchange, queue and binding are fine for normal objects. It is having this conversion exceptions for the HttpServletRequest type alone.
Controller and send method as follows:
@PostMapping("/createRequestQueue")
public ResponseEntity<String> pushRequesttoQueue(HttpServletRequest request) {
try {
rabbitMQSender.send(request);
}catch (Exception ex){
ex.printStackTrace();
}
return ResponseEntity.ok("Request received successfully");
}
public void send(HttpServletRequest request) {
try {
rabbitTemplate.convertAndSend(exchange, engRoutingkey, request);
}catch(Exception ex) {
ex.printStackTrace();
}
}
Below is the code of the receiver service trying to make a Rest Template POST call to another service in our application. The called service runs in a seperate docker container. However, it is currently being tried locally.
@RabbitListener(queues = "${spring.rabbitmq.requestqueue}")
public void receivedMessage(RequestDTO requestDto) {
logger.info("Request Received is.. " + requestDto.getFileName());
logger.debug("baseUrl to access file-service: " + baseUrl);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("X-Tenant-Identifier",requestDto.getTenantId());
String accessToken = requestDto.getAccessToken();
headers.set("Authorization", "Bearer " + accessToken);
if (logic) {
String url = baseUrl+"/processFile";
HttpEntity<RequestDTO> requestEntity = new HttpEntity<>(requestDto, headers);
ResponseEntity<ResponseDTO> resp = restTemplate.postForEntity(baseUrl, requestEntity, ResponseDTO.class);
logger.debug("Response:"+resp);
if (resp.getStatusCode() != HttpStatus.OK) {
logger.debug("Error occurred processing request:" + requestDto.getFileName());
}
}
}