Skip to content

Commit f6e4ba5

Browse files
committed
AddBook API code with headers updates
1 parent 4c30157 commit f6e4ba5

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}

src/main/java/com/learn/SpringBootRESTService/LibraryController.java

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.learn.SpringBootRESTService;
22

33
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;
48
import org.springframework.web.bind.annotation.PostMapping;
59
import org.springframework.web.bind.annotation.RequestBody;
610
import 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
}

0 commit comments

Comments
 (0)