From 00d62f516862bc51c047b79132328ddb35a9879c Mon Sep 17 00:00:00 2001 From: Neeraj Yadav Date: Wed, 11 Jul 2018 17:50:17 +0530 Subject: [PATCH 1/2] Added a POST with a request body and a DELETE method for HTTP with cURL demo --- .../web/controller/FooController.java | 111 ++++++++++-------- 1 file changed, 63 insertions(+), 48 deletions(-) diff --git a/spring-rest-simple/src/main/java/org/baeldung/web/controller/FooController.java b/spring-rest-simple/src/main/java/org/baeldung/web/controller/FooController.java index 3c7e5ed13c..31aeb0c4ae 100644 --- a/spring-rest-simple/src/main/java/org/baeldung/web/controller/FooController.java +++ b/spring-rest-simple/src/main/java/org/baeldung/web/controller/FooController.java @@ -1,48 +1,63 @@ -package org.baeldung.web.controller; - -import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; - -import org.baeldung.web.dto.Foo; -import org.baeldung.web.dto.FooProtos; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.ResponseStatus; - -@Controller -public class FooController { - - public FooController() { - super(); - } - - // API - read - - @RequestMapping(method = RequestMethod.GET, value = "/foos/{id}") - @ResponseBody - public Foo findById(@PathVariable final long id) { - return new Foo(id, randomAlphabetic(4)); - } - - // API - write - - @RequestMapping(method = RequestMethod.PUT, value = "/foos/{id}") - @ResponseStatus(HttpStatus.OK) - @ResponseBody - public Foo updateFoo(@PathVariable("id") final String id, @RequestBody final Foo foo) { - return foo; - } - - @RequestMapping(method = RequestMethod.GET, value = "/foos/{id}", produces = { "application/x-protobuf" }) - @ResponseBody - public FooProtos.Foo findProtoById(@PathVariable final long id) { - return FooProtos.Foo.newBuilder() - .setId(1) - .setName("Foo Name") - .build(); - } -} +package org.baeldung.web.controller; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; + +import org.baeldung.web.dto.Foo; +import org.baeldung.web.dto.FooProtos; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; + +@Controller +public class FooController { + + public FooController() { + super(); + } + + // API - read + + @RequestMapping(method = RequestMethod.GET, value = "/foos/{id}") + @ResponseBody + public Foo findById(@PathVariable final long id) { + return new Foo(id, randomAlphabetic(4)); + } + + // API - write + + @RequestMapping(method = RequestMethod.PUT, value = "/foos/{id}") + @ResponseStatus(HttpStatus.OK) + @ResponseBody + public Foo updateFoo(@PathVariable("id") final String id, @RequestBody final Foo foo) { + return foo; + } + + @RequestMapping(method = RequestMethod.GET, value = "/foos/{id}", produces = { "application/x-protobuf" }) + @ResponseBody + public FooProtos.Foo findProtoById(@PathVariable final long id) { + return FooProtos.Foo.newBuilder() + .setId(1) + .setName("Foo Name") + .build(); + } + + @RequestMapping(method = RequestMethod.POST, value = "/foos/new") + @ResponseStatus(HttpStatus.OK) + @ResponseBody + public Foo createFoo(@RequestBody final Foo foo) { + return foo; + } + + @RequestMapping(method = RequestMethod.DELETE, value = "/foos/{id}") + @ResponseStatus(HttpStatus.OK) + @ResponseBody + public long deleteFoo(@PathVariable final long id) { + return id; + } + +} From 838e1c2ef0433c0f3f0878d1e2813ba3f2882725 Mon Sep 17 00:00:00 2001 From: Neeraj Yadav Date: Wed, 11 Jul 2018 18:06:34 +0530 Subject: [PATCH 2/2] fixed formatting --- .../org/baeldung/web/controller/FooController.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spring-rest-simple/src/main/java/org/baeldung/web/controller/FooController.java b/spring-rest-simple/src/main/java/org/baeldung/web/controller/FooController.java index 3c7e5ed13c..7ea4dc6de0 100644 --- a/spring-rest-simple/src/main/java/org/baeldung/web/controller/FooController.java +++ b/spring-rest-simple/src/main/java/org/baeldung/web/controller/FooController.java @@ -45,4 +45,18 @@ public class FooController { .setName("Foo Name") .build(); } + + @RequestMapping(method = RequestMethod.POST, value = "/foos/new") + @ResponseStatus(HttpStatus.OK) + @ResponseBody + public Foo createFoo(@RequestBody final Foo foo) { + return foo; + } + + @RequestMapping(method = RequestMethod.DELETE, value = "/foos/{id}") + @ResponseStatus(HttpStatus.OK) + @ResponseBody + public long deleteFoo(@PathVariable final long id) { + return id; + } }