Skip to content

Instantly share code, notes, and snippets.

@martin-g
Created May 11, 2020 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martin-g/13cdfc828d6ebd6cdf6d5d5dc53a4797 to your computer and use it in GitHub Desktop.
Save martin-g/13cdfc828d6ebd6cdf6d5d5dc53a4797 to your computer and use it in GitHub Desktop.
Spring REST endpoint with CRUD operations
package info.mgsolutions.testbed.rest;
import info.mgsolutions.testbed.domain.Error;
import info.mgsolutions.testbed.domain.Person;
import info.mgsolutions.testbed.domain.Response;
import lombok.extern.slf4j.Slf4j;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;
import net.rubyeye.xmemcached.transcoders.SerializingTranscoder;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import javax.servlet.http.HttpServletRequest;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.concurrent.TimeoutException;
/**
* A REST endpoint that uses Memcached to get its data.
*/
@RestController
@RequestMapping("testbed/memcached")
@Slf4j
public class MemcachedTestController {
public static final int TTL_IN_SECONDS = 1000;
private final SerializingTranscoder coder = new SerializingTranscoder();
private final MemcachedClient client;
public MemcachedTestController(MemcachedClient client) {
this.client = client;
}
@PutMapping(value ="", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Response> create(@RequestBody Person person,
HttpServletRequest servletRequest) throws InterruptedException, MemcachedException, TimeoutException {
final String base64Name = base64(person.name);
Person existing = client.get(base64Name);
if (existing != null) {
log.info("Create: Person with name {} already exists!", person.name);
Error error = new Error("Person with name " + person.name + " already exists!");
return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE)
.body(error);
}
log.info("Create: Going to create '{}'", person);
client.set(base64Name, TTL_IN_SECONDS, person, coder);
ServletServerHttpRequest request = new ServletServerHttpRequest(servletRequest);
final UriComponents uriComponents = UriComponentsBuilder.fromHttpRequest(request).build();
final URI uri = uriComponents.encode(StandardCharsets.UTF_8).toUri();
return ResponseEntity.created(uri).contentType(MediaType.APPLICATION_JSON).body(person);
}
@GetMapping(value ="", consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Person> get(@RequestParam String name) throws InterruptedException, MemcachedException, TimeoutException {
Person person = (Person) client.get(base64(name), coder);
if (person == null) {
log.info("Get: Cannot find a person with name {}!", name);
return ResponseEntity.notFound().build();
}
log.info("Get: Found person with name: {}!", name);
return ResponseEntity.ok().body(person);
}
@PostMapping(value ="", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Person> update(@RequestBody Person person) throws InterruptedException, MemcachedException, TimeoutException {
final String name = person.name;
final String base64Name = base64(name);
final Person existing = (Person) client.get(base64Name, coder);
if (existing != null) {
log.info("Update: Going to update: {}", person);
client.set(base64Name, TTL_IN_SECONDS, person, coder);
return ResponseEntity.ok().build();
}
log.info("Update: Cannot find a person with name {}!", person.name);
return ResponseEntity.notFound().build();
}
@DeleteMapping(value ="", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Person> delete(@RequestParam String name) throws InterruptedException, MemcachedException, TimeoutException {
final String base64Name = base64(name);
final Person person = client.get(base64Name);
if (person != null) {
log.info("Delete: Going to delete: {}", person);
client.delete(base64Name);
return ResponseEntity.ok().build();
}
log.info("Delete: Cannot find a person with name {}!", name);
return ResponseEntity.notFound().build();
}
private String base64(String name) {
return Base64.getEncoder().encodeToString(name.getBytes(StandardCharsets.UTF_8));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment