Solved recursive JSON problem in RoomController.getAllDevices()

This commit is contained in:
Claudio Maggioni 2020-03-02 21:44:00 +01:00
parent 8bbcc0304f
commit 4f0351cab9
14 changed files with 78 additions and 76 deletions

View file

@ -50,6 +50,16 @@ public class SpringFoxConfig {
return List.of(new ApiKey("Bearer", "Authorization", "header")); return List.of(new ApiKey("Bearer", "Authorization", "header"));
} }
/**
* Return a Java functional API predicate for regex matches
*
* @param regex the regex to match on
* @return a Java functional API predicate
*/
private Predicate<String> regexPredicate(final String regex) {
return regex(regex)::apply;
}
/** /**
* Configures the paths the documentation must be generated for. Add a path here only when the * Configures the paths the documentation must be generated for. Add a path here only when the
* spec has been totally defined. * spec has been totally defined.
@ -57,7 +67,7 @@ public class SpringFoxConfig {
* @return A predicate that tests whether a path must be included or not * @return A predicate that tests whether a path must be included or not
*/ */
private Predicate<String> paths() { private Predicate<String> paths() {
return regex("/auth.*")::apply; return regexPredicate("/auth.*").or(regexPredicate("/room.*"));
} }
/** /**

View file

@ -1,7 +1,10 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmer; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmer;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmerRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmerRepository;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -22,8 +25,8 @@ public class ButtonDimmerController {
@Autowired private ButtonDimmerRepository buttonDimmerService; @Autowired private ButtonDimmerRepository buttonDimmerService;
@GetMapping @GetMapping
public Iterable<ButtonDimmer> findAll() { public List<ButtonDimmer> findAll() {
return buttonDimmerService.findAll(); return toList(buttonDimmerService.findAll());
} }
@GetMapping("/{id}") @GetMapping("/{id}")

View file

@ -1,7 +1,10 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLightRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLightRepository;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -23,8 +26,8 @@ public class DimmableLightController {
@Autowired private DimmableLightRepository dimmableLightService; @Autowired private DimmableLightRepository dimmableLightService;
@GetMapping @GetMapping
public Iterable<DimmableLight> findAll() { public List<DimmableLight> findAll() {
return dimmableLightService.findAll(); return toList(dimmableLightService.findAll());
} }
@GetMapping("/{id}") @GetMapping("/{id}")

View file

@ -1,7 +1,10 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmer; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmer;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmerRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmerRepository;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -23,8 +26,8 @@ public class KnobDimmerController {
@Autowired private KnobDimmerRepository knobDimmerService; @Autowired private KnobDimmerRepository knobDimmerService;
@GetMapping @GetMapping
public Iterable<KnobDimmer> findAll() { public List<KnobDimmer> findAll() {
return knobDimmerService.findAll(); return toList(knobDimmerService.findAll());
} }
@GetMapping("/{id}") @GetMapping("/{id}")

View file

@ -1,7 +1,10 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -23,8 +26,8 @@ public class MotionSensorController {
@Autowired private MotionSensorRepository motionSensorService; @Autowired private MotionSensorRepository motionSensorService;
@GetMapping @GetMapping
public Iterable<MotionSensor> findAll() { public List<MotionSensor> findAll() {
return motionSensorService.findAll(); return toList(motionSensorService.findAll());
} }
@GetMapping("/{id}") @GetMapping("/{id}")

View file

@ -1,7 +1,10 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLightRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLightRepository;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -23,8 +26,8 @@ public class RegularLightController {
@Autowired private RegularLightRepository regularLightService; @Autowired private RegularLightRepository regularLightService;
@GetMapping @GetMapping
public Iterable<RegularLight> findAll() { public List<RegularLight> findAll() {
return regularLightService.findAll(); return toList(regularLightService.findAll());
} }
@GetMapping("/{id}") @GetMapping("/{id}")

View file

@ -1,5 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RoomSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RoomSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.security.Principal; import java.security.Principal;
@ -19,15 +21,15 @@ public class RoomController {
@Autowired private UserRepository userRepository; @Autowired private UserRepository userRepository;
@Autowired private DeviceRepository deviceRepository; @Autowired private DeviceRepository<Device> deviceRepository;
@GetMapping @GetMapping
public Iterable<Room> findAll() { public List<Room> findAll() {
return roomRepository.findAll(); return toList(roomRepository.findAll());
} }
@GetMapping("/{id}") @GetMapping("/{id}")
public Optional<Room> findById(@PathVariable("id") long id) { public @ResponseBody Optional<Room> findById(@PathVariable("id") long id) {
return roomRepository.findById(id); return roomRepository.findById(id);
} }
@ -40,7 +42,6 @@ public class RoomController {
final String icon = r.getIcon(); final String icon = r.getIcon();
newRoom.setUserId(userId); newRoom.setUserId(userId);
newRoom.setUser(null);
if (img != null) { if (img != null) {
newRoom.setImage(img.getBytes()); newRoom.setImage(img.getBytes());
} else if (setWhenNull) { } else if (setWhenNull) {
@ -56,12 +57,14 @@ public class RoomController {
} }
@PostMapping @PostMapping
public Room create(@Valid @RequestBody RoomSaveRequest r, final Principal principal) { public @ResponseBody Room create(
@Valid @RequestBody RoomSaveRequest r, final Principal principal) {
return this.save(r, principal, true); return this.save(r, principal, true);
} }
@PutMapping @PutMapping
public Room update(@Valid @RequestBody RoomSaveRequest r, final Principal principal) { public @ResponseBody Room update(
@Valid @RequestBody RoomSaveRequest r, final Principal principal) {
return this.save(r, principal, false); return this.save(r, principal, false);
} }
@ -74,9 +77,8 @@ public class RoomController {
* Returns a List<Device> of all Devices that are present in a given room (identified by its * Returns a List<Device> of all Devices that are present in a given room (identified by its
* id). * id).
*/ */
@GetMapping(path = "/room/{roomid}/devices") @GetMapping(path = "/{roomId}/devices")
@ResponseBody public @ResponseBody List<? extends Device> getDevices(@PathVariable("roomId") long roomid) {
public List<Device> getDevices(@PathVariable("roomid") long roomid) {
return deviceRepository.findByRoomId(roomid); return deviceRepository.findByRoomId(roomid);
} }
} }

View file

@ -1,7 +1,10 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*; import java.util.*;
import java.util.List;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.*; import org.springframework.boot.autoconfigure.*;
@ -15,8 +18,8 @@ public class SensorController {
@Autowired private SensorRepository sensorRepository; @Autowired private SensorRepository sensorRepository;
@GetMapping @GetMapping
public Iterable<Sensor> findAll() { public List<Sensor> findAll() {
return sensorRepository.findAll(); return toList(sensorRepository.findAll());
} }
@GetMapping("/{id}") @GetMapping("/{id}")

View file

@ -1,7 +1,10 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*; import java.util.*;
import java.util.List;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.*; import org.springframework.boot.autoconfigure.*;
@ -15,8 +18,8 @@ public class SmartPlugController {
@Autowired private SmartPlugRepository smartPlugRepository; @Autowired private SmartPlugRepository smartPlugRepository;
@GetMapping @GetMapping
public Iterable<SmartPlug> findAll() { public List<SmartPlug> findAll() {
return smartPlugRepository.findAll(); return toList(smartPlugRepository.findAll());
} }
@GetMapping("/{id}") @GetMapping("/{id}")

View file

@ -1,7 +1,10 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*; import java.util.*;
import java.util.List;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.*; import org.springframework.boot.autoconfigure.*;
@ -15,8 +18,8 @@ public class SwitchController {
@Autowired private SwitchRepository switchRepository; @Autowired private SwitchRepository switchRepository;
@GetMapping @GetMapping
public Iterable<Switch> findAll() { public List<Switch> findAll() {
return switchRepository.findAll(); return toList(switchRepository.findAll());
} }
@GetMapping("/{id}") @GetMapping("/{id}")

View file

@ -26,12 +26,6 @@ public abstract class Device {
@ApiModelProperty(hidden = true) @ApiModelProperty(hidden = true)
private long id; private long id;
/** The room this device belongs in */
@ManyToOne
@JoinColumn(name = "room_id", nullable = false, updatable = false, insertable = false)
@ApiModelProperty(hidden = true)
private Room room;
/** /**
* The room this device belongs in, as a foreign key id. To use when updating and inserting from * The room this device belongs in, as a foreign key id. To use when updating and inserting from
* a REST call. * a REST call.
@ -69,14 +63,6 @@ public abstract class Device {
this.id = id; this.id = id;
} }
public Room getRoom() {
return room;
}
public void setRoom(Room room) {
this.room = room;
}
public String getName() { public String getName() {
return name; return name;
} }

View file

@ -1,7 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@ -42,17 +41,6 @@ public class Room {
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;
/** Collection of devices present in this room */
@ApiModelProperty(hidden = true)
@OneToMany(mappedBy = "room")
private Set<Device> devices;
/** User that owns the house this room is in */
@ManyToOne
@JoinColumn(name = "user_id", nullable = false, updatable = false, insertable = false)
@ApiModelProperty(hidden = true)
private User user;
public Long getId() { public Long getId() {
return id; return id;
} }
@ -69,14 +57,6 @@ public class Room {
this.userId = userId; this.userId = userId;
} }
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getName() { public String getName() {
return name; return name;
} }
@ -85,10 +65,6 @@ public class Room {
this.name = name; this.name = name;
} }
public Set<Device> getDevices() {
return devices;
}
public byte[] getIcon() { public byte[] getIcon() {
return icon; return icon;
} }
@ -107,6 +83,6 @@ public class Room {
@Override @Override
public String toString() { public String toString() {
return "Room{" + "id=" + id + ", user=" + user + ", name='" + name + "\'}"; return "Room{" + "id=" + id + ", name='" + name + "\'}";
} }
} }

View file

@ -1,7 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.*; import javax.validation.constraints.*;
@ -49,11 +48,6 @@ public class User {
@Pattern(regexp = ".+@.+\\..+", message = "Please provide a valid email address") @Pattern(regexp = ".+@.+\\..+", message = "Please provide a valid email address")
private String email; private String email;
/** All rooms in the user's house */
@OneToMany(mappedBy = "user")
@ApiModelProperty(hidden = true)
private Set<Room> rooms;
public Long getId() { public Long getId() {
return id; return id;
} }
@ -94,10 +88,6 @@ public class User {
this.password = password; this.password = password;
} }
public Set<Room> getRooms() {
return rooms;
}
@Override @Override
public String toString() { public String toString() {
return "User{" return "User{"

View file

@ -0,0 +1,14 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.utils;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
/** A class with a bunch of useful static methods */
public class Utils {
private Utils() {}
public static <T> List<T> toList(Iterable<T> iterable) {
return StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toList());
}
}