Skip to content

Commit 00d62f5

Browse files
committed
Added a POST with a request body and a DELETE method for HTTP with cURL demo
1 parent 88f832b commit 00d62f5

1 file changed

Lines changed: 63 additions & 48 deletions

File tree

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,63 @@
1-
package org.baeldung.web.controller;
2-
3-
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
4-
5-
import org.baeldung.web.dto.Foo;
6-
import org.baeldung.web.dto.FooProtos;
7-
import org.springframework.http.HttpStatus;
8-
import org.springframework.stereotype.Controller;
9-
import org.springframework.web.bind.annotation.PathVariable;
10-
import org.springframework.web.bind.annotation.RequestBody;
11-
import org.springframework.web.bind.annotation.RequestMapping;
12-
import org.springframework.web.bind.annotation.RequestMethod;
13-
import org.springframework.web.bind.annotation.ResponseBody;
14-
import org.springframework.web.bind.annotation.ResponseStatus;
15-
16-
@Controller
17-
public class FooController {
18-
19-
public FooController() {
20-
super();
21-
}
22-
23-
// API - read
24-
25-
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}")
26-
@ResponseBody
27-
public Foo findById(@PathVariable final long id) {
28-
return new Foo(id, randomAlphabetic(4));
29-
}
30-
31-
// API - write
32-
33-
@RequestMapping(method = RequestMethod.PUT, value = "/foos/{id}")
34-
@ResponseStatus(HttpStatus.OK)
35-
@ResponseBody
36-
public Foo updateFoo(@PathVariable("id") final String id, @RequestBody final Foo foo) {
37-
return foo;
38-
}
39-
40-
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}", produces = { "application/x-protobuf" })
41-
@ResponseBody
42-
public FooProtos.Foo findProtoById(@PathVariable final long id) {
43-
return FooProtos.Foo.newBuilder()
44-
.setId(1)
45-
.setName("Foo Name")
46-
.build();
47-
}
48-
}
1+
package org.baeldung.web.controller;
2+
3+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
4+
5+
import org.baeldung.web.dto.Foo;
6+
import org.baeldung.web.dto.FooProtos;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.web.bind.annotation.PathVariable;
10+
import org.springframework.web.bind.annotation.RequestBody;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RequestMethod;
13+
import org.springframework.web.bind.annotation.ResponseBody;
14+
import org.springframework.web.bind.annotation.ResponseStatus;
15+
16+
@Controller
17+
public class FooController {
18+
19+
public FooController() {
20+
super();
21+
}
22+
23+
// API - read
24+
25+
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}")
26+
@ResponseBody
27+
public Foo findById(@PathVariable final long id) {
28+
return new Foo(id, randomAlphabetic(4));
29+
}
30+
31+
// API - write
32+
33+
@RequestMapping(method = RequestMethod.PUT, value = "/foos/{id}")
34+
@ResponseStatus(HttpStatus.OK)
35+
@ResponseBody
36+
public Foo updateFoo(@PathVariable("id") final String id, @RequestBody final Foo foo) {
37+
return foo;
38+
}
39+
40+
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}", produces = { "application/x-protobuf" })
41+
@ResponseBody
42+
public FooProtos.Foo findProtoById(@PathVariable final long id) {
43+
return FooProtos.Foo.newBuilder()
44+
.setId(1)
45+
.setName("Foo Name")
46+
.build();
47+
}
48+
49+
@RequestMapping(method = RequestMethod.POST, value = "/foos/new")
50+
@ResponseStatus(HttpStatus.OK)
51+
@ResponseBody
52+
public Foo createFoo(@RequestBody final Foo foo) {
53+
return foo;
54+
}
55+
56+
@RequestMapping(method = RequestMethod.DELETE, value = "/foos/{id}")
57+
@ResponseStatus(HttpStatus.OK)
58+
@ResponseBody
59+
public long deleteFoo(@PathVariable final long id) {
60+
return id;
61+
}
62+
63+
}

0 commit comments

Comments
 (0)