Skip to content

Commit

Permalink
use json type response instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
bmudric committed Jul 5, 2020
1 parent 2b1f56d commit 8549db7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
18 changes: 18 additions & 0 deletions src/main/java/com/jazavac/relaysim/RelayResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.jazavac.relaysim;

public class RelayResponse {

private boolean relayIsOn;

public RelayResponse(boolean relayIsOn) {
this.relayIsOn = relayIsOn;
}

public boolean isRelayIsOn() {
return relayIsOn;
}

public void setRelayIsOn(boolean relayIsOn) {
this.relayIsOn = relayIsOn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Slf4j
@SpringBootApplication
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.jazavac.relaysim.controller;

import javax.servlet.http.HttpServletResponse;
import com.jazavac.relaysim.RelayResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/** This class is used to simulate getting a status of a relay that turns on and off every second */
@RestController
@CrossOrigin
@Slf4j
public class RandomStatusController {

@GetMapping("/randomstatus")
public String returnStatus(HttpServletResponse response) {
public RelayResponse returnStatus() {
log.debug("Client requesting GET /randomstatus");
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Content-Type", "application/json");
return String.format("{\"relayIsOn\":%b}", generateStatus());
return new RelayResponse(generateStatus());
}

private boolean generateStatus() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jazavac.relaysim.controller;

import com.jazavac.relaysim.RelayResponse;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -19,22 +19,22 @@ public class RelayApiController {
private AtomicBoolean relayIsOn = new AtomicBoolean(false);

@GetMapping("/status")
public String returnStatus(HttpServletResponse response) {
public RelayResponse returnStatus() {
log.debug("Client requesting GET /status");
return String.format("{\"relayIsOn\":%b}", relayIsOn.get());
return new RelayResponse(relayIsOn.get());
}

@PostMapping("/on")
public String handleOn(HttpServletResponse response) {
public RelayResponse handleOn() {
log.debug("Client requesting POST /on");
relayIsOn.set(true);
return returnStatus(response);
return returnStatus();
}

@PostMapping("/off")
public String handleOff(HttpServletResponse response) {
public RelayResponse handleOff() {
log.debug("Client requesting POST /off");
relayIsOn.set(false);
return returnStatus(response);
return returnStatus();
}
}

0 comments on commit 8549db7

Please sign in to comment.