первая лаба сделана
This commit is contained in:
parent
db166df53b
commit
89778a0f59
@ -17,6 +17,8 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-actuator'
|
||||
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,63 @@
|
||||
package com.example.backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
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.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class BackendApplication {
|
||||
|
||||
private ArrayList<TestDTO> list = new ArrayList<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BackendApplication.class, args);
|
||||
}
|
||||
|
||||
@GetMapping("/hello")
|
||||
public String get(@RequestParam(name = "name", defaultValue = "World!") String name){
|
||||
return String.format("Hello, %s", name);
|
||||
}
|
||||
|
||||
@GetMapping()
|
||||
public ArrayList<TestDTO> getAll(){
|
||||
return list;
|
||||
}
|
||||
|
||||
@GetMapping("/id")
|
||||
public TestDTO getById(@RequestParam(name = "id") int id){
|
||||
return list.get(id);
|
||||
}
|
||||
|
||||
@PostMapping()
|
||||
public TestDTO postTestDTO(@RequestBody TestDTO testDTO) {
|
||||
list.add(testDTO);
|
||||
return testDTO;
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public TestDTO updaTestDTO(@PathVariable(name = "id") int id, @RequestBody TestDTO entity) {
|
||||
list.add(id, entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
|
||||
@DeleteMapping("{id}")
|
||||
public TestDTO delMethod(@PathVariable(name = "id") int id){
|
||||
return list.remove(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
55
backend/src/main/java/com/example/backend/TestDTO.java
Normal file
55
backend/src/main/java/com/example/backend/TestDTO.java
Normal file
@ -0,0 +1,55 @@
|
||||
package com.example.backend;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class TestDTO {
|
||||
|
||||
private Integer id;
|
||||
private String name;
|
||||
private String img;
|
||||
|
||||
public TestDTO() {
|
||||
|
||||
}
|
||||
|
||||
@JsonCreator
|
||||
public TestDTO(
|
||||
@JsonProperty(value = "id") Integer id,
|
||||
@JsonProperty(value = "name") String name,
|
||||
@JsonProperty(value = "img") String img
|
||||
)
|
||||
{
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.img = img;
|
||||
}
|
||||
|
||||
// GET
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getImg(){
|
||||
return img;
|
||||
}
|
||||
|
||||
// SET
|
||||
|
||||
public void setId(Integer Id) {
|
||||
id = Id;
|
||||
}
|
||||
|
||||
public void setName(String Name) {
|
||||
name = Name;
|
||||
}
|
||||
|
||||
public void setImg(String Img){
|
||||
img = Img;
|
||||
}
|
||||
}
|
16
backend/src/main/java/com/example/backend/WebConfig.java
Normal file
16
backend/src/main/java/com/example/backend/WebConfig.java
Normal file
@ -0,0 +1,16 @@
|
||||
package com.example.backend;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer{
|
||||
@Override
|
||||
public void addCorsMappings(@NonNull CorsRegistry registry){
|
||||
registry.addMapping("/**")
|
||||
.allowedMethods("GET", "POST", "PUT", "DELETE");
|
||||
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user