Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot: Return a empty JSON instead of empty body when returned object is null

Tags:

json

rest

spring

I have a RestController and when I call the method:

@RequestMapping(value = "/sigla/{sigla}")
@ResponseBody
public PaisDTO obterPorSigla(@PathVariable String sigla) {
    return service.obterPorSigla(sigla);
}

If a record is found, I get a good JSON response:

{"nome":"Brasil","sigla":"BR","quantidadeEstados":27}

but when nothing is found on database the RestController returns null and I get a empty response, completely blank body.

How can I display a empty JSON instead of a blank response? Like bellow:

{}

Complete Controller:

@RestController
@RequestMapping("/pais")
public class PaisController {

    @Autowired
    private PaisService service;

    @RequestMapping
    public ResponseEntity<List<PaisDTO>> obterTodos() {
        return CreateResponseEntity.getResponseEntity(service.obterTodos());
    }

    @RequestMapping(value = "/sigla/{sigla}", method = RequestMethod.GET, consumes="application/json", produces="application/json")
    public ResponseEntity<PaisDTO> obterPorSigla(@PathVariable String sigla) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        PaisDTO paisDTO = service.obterPorSigla(sigla);
        if(paisDTO != null) return new ResponseEntity<PaisDTO>(paisDTO, headers, HttpStatus.OK);
        else return new ResponseEntity<PaisDTO>(headers, HttpStatus.OK);
    }
like image 804
Hélio Márcio Filho Avatar asked Jun 30 '17 02:06

Hélio Márcio Filho


People also ask

How do you make an empty JSON object?

detailJSON=null ; detailJSON=new JSONObject();

How check response JSON is empty?

If you want to check if your response is not empty try : if ( json. length == 0 ) { console. log("NO DATA!") }


2 Answers

First, if you're using @RestController annotation you don't need the @ResponseBody annotation, get rid of that.

Second if you're trying to have REST Controller, then you're missing a few things, do it like this:

@RequestMapping(value = "/sigla/{sigla}", method = RequestMethod.GET, consumes = "application/json", produces="application/json")
public ResponseEntity<PaisDTO> obterPorSigla(@PathVariable String sigla) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");    
        PaisDTO paisDTO = service.obterPorSigla(sigla);
        if(paisDTO != null) return new ResponseEntity<>(paisDTO, headers, HttpStatus.OK);
        else return new ResponseEntity<>(headers, HttpStatus.OK); 
}

In the example above if you'll get null then you'll return an empty response JSON.

like image 138
Moshe Arad Avatar answered Sep 20 '22 16:09

Moshe Arad


Solution 1: You have to implement you entity class with Serializable Solution 2: Your class should have getter and setter In my case the getter and setter were given protected access modifiers. so I changed them to public and vola it worked

like image 28
Chandu Avatar answered Sep 18 '22 16:09

Chandu