1

I have a spring MVC config with the following:

       public class SpringConfiguration extends WebMvcConfigurerAdapter {
        public MappingJackson2HttpMessageConverter jacksonMessageConverter() {
            MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();

            ObjectMapper mapper = new ObjectMapper();
            //Registering Hibernate4Module to support lazy objects
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            mapper.registerModule(new Hibernate4Module());

            messageConverter.setObjectMapper(mapper);
            return messageConverter;
        }
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //Here we add our custom-configured HttpMessageConverter
        converters.add(jacksonMessageConverter());
        super.configureMessageConverters(converters);
        }
      }

The previous method used to ignore all lazy relation without adding JsonIgnore in model The problem is I have a route to steam mp3 file as an octet response as following

@GetMapping(value = "/audio/{id}")
public ResponseEntity<byte[]> streamMp3FileToAdmin(@PathVariable Integer id) {
    CorporateCampaign camp = corporateCampaignService.findById(id);
    final HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    return new ResponseEntity<byte[]>(Utilities.getFileAsBytes(camp.getVoiceUrl()),httpHeaders,HttpStatus.OK);
}

If I remove jackson message converter the steaming works fine but when I add jackson message converter the stream doesn't work any more

I read this question Spring MVC: How to return image in @ResponseBody?

and a lot but I didn't find a solution yet

Allloush
  • 1,140
  • 8
  • 27

1 Answers1

1

You need to add produces = MediaType.APPLICATION_OCTET_STREAM to the @GetMapping(value = "/audio/{id}") to specify produced result content type and let browser recognize it properly.

StanislavL
  • 56,971
  • 9
  • 68
  • 98