1 лабораторная
This commit is contained in:
parent
70b7453473
commit
dd8db2f0d1
@ -1,62 +1,53 @@
|
||||
package com.example.demo;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
@RequestMapping("/api/book")
|
||||
public class ApiController {
|
||||
private final Logger log = LoggerFactory.getLogger(ApiController.class);
|
||||
private final Map<Integer, Books> book = new HashMap<>();
|
||||
|
||||
@GetMapping
|
||||
public String get(@RequestParam(name = "name", defaultValue = "World") String name) {
|
||||
return String.format("Hello, %s!", name);
|
||||
}
|
||||
|
||||
@GetMapping("/test")
|
||||
public String getTest() {
|
||||
return new Date().toString();
|
||||
}
|
||||
|
||||
@GetMapping("/num")
|
||||
public Integer getNum() {
|
||||
return 10;
|
||||
}
|
||||
|
||||
@GetMapping("/list")
|
||||
public List<Integer> getList() {
|
||||
return List.of(10, 20, 30, 40, 50);
|
||||
}
|
||||
|
||||
@GetMapping("/map")
|
||||
public TestDto getMap() {
|
||||
return new TestDto("Ivanov", "Ivan");
|
||||
}
|
||||
|
||||
@PostMapping("/map")
|
||||
public TestDto postMap(@RequestBody TestDto testDto) {
|
||||
return testDto;
|
||||
@GetMapping("/{id}")
|
||||
public Books getBook(@PathVariable(name = "id") int id) {
|
||||
return book.get(id);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public String postMethodName(@RequestBody String name) {
|
||||
return get(name);
|
||||
public Books postBook(@RequestBody Books entity) {
|
||||
book.put(book.size(), entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@PutMapping("/{name}")
|
||||
public String putMethodName(@PathVariable(name = "name") String name, @RequestBody String entity) {
|
||||
log.info("The body value is {}", entity);
|
||||
return get(name);
|
||||
@PutMapping("/{id}")
|
||||
public Books putMethodBook(@PathVariable(name = "id") int id, @RequestBody Books entity) {
|
||||
book.replace(id, entity);
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<Books> getMethodAll() {
|
||||
return List.copyOf(book.values());
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public Books delMethod(@PathVariable(name = "id") int id) {
|
||||
Books old = book.get(id);
|
||||
book.remove(id);
|
||||
return old;
|
||||
}
|
||||
}
|
47
demo/src/main/java/com/example/demo/Books.java
Normal file
47
demo/src/main/java/com/example/demo/Books.java
Normal file
@ -0,0 +1,47 @@
|
||||
package com.example.demo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class Books {
|
||||
private String name;
|
||||
private double price;
|
||||
private int typesId;
|
||||
private int authorsId;
|
||||
private String image;
|
||||
|
||||
@JsonCreator
|
||||
public Books(
|
||||
@JsonProperty(value = "name") String name,
|
||||
@JsonProperty(value = "price") double price,
|
||||
@JsonProperty(value = "typesId") int typesId,
|
||||
@JsonProperty(value = "authorsId") int authorsId,
|
||||
@JsonProperty(value = "image") String image) {
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
this.typesId = typesId;
|
||||
this.authorsId = authorsId;
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public int getTypesId() {
|
||||
return typesId;
|
||||
}
|
||||
|
||||
public int getAuthorsId() {
|
||||
return authorsId;
|
||||
}
|
||||
|
||||
public String getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package com.example.demo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class TestDto {
|
||||
private String shortName;
|
||||
private String fullName;
|
||||
|
||||
public TestDto() {
|
||||
}
|
||||
|
||||
@JsonCreator
|
||||
public TestDto(
|
||||
@JsonProperty(value = "shortName") String shortName,
|
||||
@JsonProperty(value = "fullName") String fullName) {
|
||||
this.shortName = shortName;
|
||||
this.fullName = fullName;
|
||||
}
|
||||
|
||||
public String getShortName() {
|
||||
return shortName;
|
||||
}
|
||||
|
||||
public String getFullName() {
|
||||
return fullName;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user