Responsestatusexception - Spring boot - Return of the message

Asked

Viewed 1,098 times

0

I am new to java and I am created a training API and I came across the following problem:

I do the request via Postman, the return of JSON is ok, but if I do a search that does not return anything the message template below is returned:


    {
        "timestamp": "2020-06-04T23:52:13.722+00:00",
        "status": 404,
        "error": "Not Found",
        "message": "",
        "path": "/api/produtos/2"
    }

So far so good, what happens is that the attribute message is not completed.

Use the org.springframework.web.server.ResponseStatusException.ResponseStatusException spring.

        @RestController
        @RequestMapping("/api/produtos/")
        public class ProdutoController {
            @Autowired
            ProdutoService produtoService;

            @GetMapping("{id}")
            public Produto getById(@PathVariable Integer id) {
                return produtoService.findById(id)
                        .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Produto não encontrado."));

            }
        }

        @Service
        public class ProdutoService {
           @Autowired
           ProdutoRepository produtoRepository;

           public Optional<Produto> findById(Integer id){
              return produtoRepository.findById(id);
            }
        }
  • which version you are using of spring boot?

  • I used version v2.3.0.RELEASE and the problem is there.

  • See my answer.

2 answers

0

I did the test customizing an Exception but it didn’t work.

It’s not the most elegant way but it solves your problem.

@RestController
@RequestMapping("product")
public class ProductController {

    @GetMapping("{id}")
    public ResponseEntity getById(@PathVariable Integer id) {

        Optional<Produto> product = new Service().findById(id);

        if(!product.isPresent()) {
            return new ResponseEntity(new Erro("produto nao encontrado"), HttpStatus.NOT_FOUND);
        }

        return new ResponseEntity(product, HttpStatus.FOUND);
    }
}

class Produto {
    public long id;
}

class Service{
    public Optional<Produto> findById(Integer id){
        return Optional.empty();
    }
}

class Erro{
    public String message;

    public Erro(String message) {
        this.message = message;
    }
}
  • 1

    Thanks for the solution, with the procedure that Patrick posted!

0


By default, sprint boot does not show this variable. You can check the default values here: Spring Boot Reference Documentation.

To solve this problem, just add in your application.yaml:

server:
  error:
    include-message: always

or in your application.properties:

server.error.include-message=always

I just tested your code and it worked correctly:

{
    "timestamp": "2020-06-05T10:56:36.008+00:00",
    "status": 404,
    "error": "Not Found",
    "message": "Produto não encontrado.",
    "path": "/api/produtos/2"
}
  • Thank you Patrick, the problem has been solved!

Browser other questions tagged

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