简体   繁体   中英

In java/spring-boot what is the cleanest way to return an empty json object if an item is not found

Below is my Java/spring-boot controller code for /api/speaker/123 request handler:

@GetMapping
@RequestMapping("{id}")
public Speaker get(@PathVariable Long id) {
    return speakerRepository.getOne(id);
}

If the id 123 is not found for the incoming request /api/speaker/123 , then how to quickly code to return an empty object/json to the browser? In Nodejs/JavaScript world, I could simply do below one line. Any such clean code without having to add if...else then, more code to look for null/empty , then do more...and then return!

return speakerRepository.getOne(id) || {};

Edited:

// Speaker.java
@Entity(name = "speakers")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Speaker {
    public Long getSpeaker_id() {
        return speaker_id;
    }

    public void setSpeaker_id(Long speaker_id) {
        this.speaker_id = speaker_id;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getSpeaker_bio() {
        return speaker_bio;
    }

    public void setSpeaker_bio(String speaker_bio) {
        this.speaker_bio = speaker_bio;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long speaker_id;
    private String first_name;
    private String last_name;
    private String title;
    private String company;
    private String speaker_bio;

    public List<Session> getSessions() {
        return sessions;
    }

    @ManyToMany(mappedBy = "speakers")
    @JsonIgnore
    private List<Session> sessions;
    public Speaker(){

    }
}




// SpeakerRepository.java
public interface SpeakerRepository extends JpaRepository<Speaker, Long> {       
}
@GetMapping
@RequestMapping("{id}")
public Speaker get(@PathVariable Long id) {
    return speakerRepository.getOne(id);
}

instead use below code:

@JsonSerialize
public class EmptyJsonResponse {}



@GetMapping
@RequestMapping("{id}")
public ResponseEntity<?> get(@PathVariable Long id) {
   Speaker speaker = speakerRepository.getOne(id);
    if(speaker != null){
      return new ResponseEntity<Speaker>(speaker, HttpStatus.OK)
    }else{
      return new ResponseEntity<EmptyJsonResponse>(new EmptyJsonResponse(), HttpStatus.OK)
   }         
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM