Difficulty Working with Thread in Spring Boot

Asked

Viewed 79 times

1

See how my controller is

@PostMapping("/anexo")
    public DeferredResult<String> uploadAnexo(@RequestParam("files[]") MultipartFile[] files){
        DeferredResult<String> resultado = new DeferredResult<>();

        // retorno assíncrono
        Thread thread = new Thread(new FotoStorageRunnable(files, resultado));
        thread.start();

        return resultado;
    }

And look at my Thread

import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.multipart.MultipartFile;

public class FotoStorageRunnable implements Runnable {

    private MultipartFile[] files;
    private DeferredResult<String> resultado;   


    public FotoStorageRunnable(MultipartFile[] files, DeferredResult<String> resultado) {
        this.files = files;
        this.resultado = resultado;
    }

    @Override
    public void run() {
        System.out.println(">>> files: " + files[0].getSize());
        resultado.setResult("ok! esse é o resultado foto recebida");

    }

}

This message should have arrived on the server;

System.out.println(">>> files: " + files[0].getSize());

And these messages should have reached the customer;

resultado.setResult("ok! esse é o resultado foto recebida");

Neither of these two messages arrived and still generated this error message just below;

inserir a descrição da imagem aqui

I wonder where it’s wrong?

1 answer

0

The uploadAnexo method sends a parameter of type Multipartfile[] to the constructor of class Fotostoragerunnable, apparently this parameter is going null, since the Exception launched is that there is nothing at position 0 of the array.

  • That’s the explanation of the error message, you’d have a suggestion how to get around it?

  • The error you are generating is 503, it is a server error.

  • I just made a new test, I took the line of code that is on line 19, then the message that is on line 20 went to the client, so the problem is on line 19, I would have to know why the variable files is in trouble.

  • I am following this line of code https://github.com/algaworks/curso-sistemas-web-com-spring-javascript-bootstrap/blob/master/14.4-besting-availabilitye-da-application-buter/brewer/src/mainjava/comalgaworks/brewer/controller/controller/FotosController.java

Browser other questions tagged

You are not signed in. Login or sign up in order to post.