0

I was trying to transmit objects between two controllers with StreamingResponseBody. All was pretty much good to me until I noticed that I transferring just a regular String. Its bytes are different from that ones I am sending from /send endpoint...

I would be geatfull if you can help me to figure out what is going on.

SendController:

... 

@GetMapping(value="/send")
public ResponseEntity<StreamingResponseBody> send(){

 List<Student> students = studentManager.getStudents();

 StreamingResponseBody stream = outputStream -> {
  for(Student student: students){
    byte[] data = SerializationUtils.serialize(student);
    outputStream.write(data);
    outputStream.flush();
  }
 };

 ResponseEntity.ok()
   .contentType(MediaType.APPLICATION_STREAM_JSON)
   .body(stream);
}

Another app, Receivier method:

// code ommited
...

HttpHeaders headers = new HttpHeadres();
List<MediaType> mediaList = new ArrayList<>();
headers.setAccept(mediaList);

HttpEntity<Object> entity = new HttpEntity<>(null, headers)
ResponseEntity<String> response = new restTemplate.exchange("http://localhost:8080/send", HttpMethod.GET, entity, String.class);

byte[] bytes = response.getBody().getBytes();
Object responseObj = SerializationUtils.deserialize(bytes);

List<Object> objs = Arrays.asList(responseObj);
List<Student> students = new ArrayList<>();
for(Object o: objs){
 students.add((Students)o);
}

...
Amadis
  • 11
  • 2
  • If you are transferring arbitrary binary data, you shouldn't be reading that directly as a string. Strings are for text data, not arbitrary binary data. (You can encode arbitrary binary data in text using things like base64, but there's no indication that you're doing that.) – Jon Skeet Oct 10 '22 at 15:00
  • @JonSkeet Oh, I see what you mean. I'll try this now... – Amadis Oct 10 '22 at 15:09

1 Answers1

0

You are using a lambda. You have neither a guarantee that the lambda is executed during the send() method, nor that the same thread is used.

Since the transactional context used by Spring is stored in a thread-local variable, you might loose this transactional-context. If student is a entity-instance in a transactional-session student might not be deserialized compleatly from the database anymore.

Grim
  • 1,938
  • 10
  • 56
  • 123