File tree 2 files changed +46
-2
lines changed
src/main/java/com/learn/SpringBootRESTService
2 files changed +46
-2
lines changed Original file line number Diff line number Diff line change
1
+ package com .learn .SpringBootRESTService ;
2
+
3
+ import org .springframework .stereotype .Component ;
4
+
5
+ @ Component
6
+ public class AddResponse {
7
+
8
+
9
+ String msg ;
10
+ String id ;
11
+ public String getMsg () {
12
+ return msg ;
13
+ }
14
+ public void setMsg (String msg ) {
15
+ this .msg = msg ;
16
+ }
17
+ public String getId () {
18
+ return id ;
19
+ }
20
+ public void setId (String id ) {
21
+ this .id = id ;
22
+ }
23
+
24
+
25
+ }
Original file line number Diff line number Diff line change 1
1
package com .learn .SpringBootRESTService ;
2
2
3
3
import org .springframework .beans .factory .annotation .Autowired ;
4
+ import org .springframework .http .HttpHeaders ;
5
+ import org .springframework .http .HttpStatus ;
6
+ import org .springframework .http .HttpStatusCode ;
7
+ import org .springframework .http .ResponseEntity ;
4
8
import org .springframework .web .bind .annotation .PostMapping ;
5
9
import org .springframework .web .bind .annotation .RequestBody ;
6
10
import org .springframework .web .bind .annotation .RestController ;
@@ -13,9 +17,24 @@ public class LibraryController {
13
17
@ Autowired
14
18
LibraryRepository repository ;
15
19
20
+ @ Autowired
21
+ AddResponse addResponse ;
22
+
23
+
16
24
@ PostMapping ("/addBook" )
17
- public void addBookImplementation (@ RequestBody Library library ) {
18
- library .setId (library .getIsbn ()+library .getAisle ());
25
+ public ResponseEntity addBookImplementation (@ RequestBody Library library ) {
26
+ String id = library .getIsbn ()+library .getAisle ();
27
+ library .setId (id );
19
28
repository .save (library );
29
+ addResponse .setMsg ("Success!! Book is added" );
30
+ addResponse .setId (id );
31
+ HttpHeaders headers = new HttpHeaders ();
32
+ headers .add ("Unique-Header" , "First" );
33
+ //return addResponse; without response headers!
34
+ return new ResponseEntity <AddResponse >(addResponse ,headers ,HttpStatus .CREATED );
35
+
20
36
}
37
+
38
+
39
+
21
40
}
You can’t perform that action at this time.
0 commit comments