File tree Expand file tree Collapse file tree
src/main/java/com/learn/SpringBootRESTService Expand file tree Collapse file tree 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 11package com .learn .SpringBootRESTService ;
22
33import 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 ;
48import org .springframework .web .bind .annotation .PostMapping ;
59import org .springframework .web .bind .annotation .RequestBody ;
610import org .springframework .web .bind .annotation .RestController ;
@@ -13,9 +17,24 @@ public class LibraryController {
1317 @ Autowired
1418 LibraryRepository repository ;
1519
20+ @ Autowired
21+ AddResponse addResponse ;
22+
23+
1624 @ 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 );
1928 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+
2036 }
37+
38+
39+
2140}
You can’t perform that action at this time.
0 commit comments