первая лаба сделана

This commit is contained in:
ekallin 2024-03-16 10:50:39 +04:00
parent db166df53b
commit 89778a0f59
4 changed files with 124 additions and 1 deletions

View File

@ -16,7 +16,9 @@ repositories {
} }
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' 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' testImplementation 'org.springframework.boot:spring-boot-starter-test'
} }

View File

@ -1,13 +1,63 @@
package com.example.backend; package com.example.backend;
import java.util.ArrayList;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; 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 @SpringBootApplication
@RestController
@RequestMapping("/api")
public class BackendApplication { public class BackendApplication {
private ArrayList<TestDTO> list = new ArrayList<>();
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(BackendApplication.class, 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);
}
} }

View 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;
}
}

View 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");
}
}