close

Filter

loading table of contents...

Studio Developer Manual / Version 2301

Table Of Contents

9.31.5 REST Linking (Java Backend)

The note example has shown how to create a custom remote bean. However, in the real world you usually have to deal with a list of remote beans, so let's improve the example by adding another EntityController that is responsible for loading a list of notes.

First of all, you have to create the required Java classes for this. The model could look like this:

public class NoteList {
  private List<Note> notes = new ArrayList<>();

  public List<Note> getNotes() {
    return notes;
  }

  public void setNotes(List<Note> notes) {
    this.notes = notes;
  }
}

Example 9.131. Java class for notes list


Next, the matching representation which looks the same again:

public class NotesRepresentation {
  private List<Note> notes;

  public NotesRepresentation(NoteList noteList) {
    this.notes = noteList.getNotes();
  }

  public List<Note> getNotes() {
    return notes;
  }
}

Example 9.132. Notes list representation


So you can create the EntityController from it:

@RestController
@RequestMapping(value = "notes", produces = MediaType.APPLICATION_JSON_VALUE)
public class NotesEntityController implements EntityController<NoteList> {

  private final NotesService notesService;

  public NotesEntityController(NotesService notesService) {
    this.notesService = notesService;
  }

  @Override
  @NonNull
  public NoteList getEntity(@NonNull Map<String, String> pathVariables) {
    NoteList noteList = new NoteList();
    noteList.setNotes(notesService.getNotes());
    return noteList;
  }

  @GetMapping
  public NotesRepresentation getRepresentation(@NonNull Map<String, String> pathVariables) {
    NoteList entity = getEntity(pathVariables);
    return new NotesRepresentation(entity);
  }

  @PutMapping
  public ResponseEntity<NotesRepresentation> setRepresentation(@NonNull Map<String, String> pathVariables,
                                                               @RequestBody final Map<String, Object> json) {
    //noinspection unchecked
    notesService.setNotes((List<Note>) json.get("notes"));
    NoteList entity = getEntity(pathVariables);
    return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(new NotesRepresentation(entity));
  }
}

Example 9.133. NotesEntityController for notes list


The example returns a list of all notes of the NotesService. In addition to this, you can change the list. Have a look at the code of the put mapping:

@PutMapping
public ResponseEntity<NotesRepresentation> setRepresentation(@NonNull Map<String, String> pathVariables,
                                                             @RequestBody final Map<String, Object> json) {
  //noinspection unchecked
  notesService.setNotes((List<Note>) json.get("notes"));
  NoteList entity = getEntity(pathVariables);
  return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(new NotesRepresentation(entity));
}

Example 9.134. Put mapping for notes list


The changed properties of the NotesList will be passed via the RequestBody. You can expect that its structure is the same as in the NotesRepresentation, so the property notes will contain the changed list of notes.

As a last step, you have to add the Spring bean to your Spring configuration:

@Bean
public NotesEntityController notesEntityController(NotesService notesService) {
  return new NotesEntityController(notesService);
}

Example 9.135. Adding a Spring bean to Spring configuration


Again, the Java part is finished and you can rebuild the extension and restart Studio.

Search Results

Table Of Contents
warning

Your Internet Explorer is no longer supported.

Please use Mozilla Firefox, Google Chrome, or Microsoft Edge.