Compare commits

...

1 Commits

Author SHA1 Message Date
0fb9e271ee LabWork01 2024-03-06 18:55:33 +04:00
5 changed files with 186 additions and 0 deletions

View File

@ -17,6 +17,8 @@ repositories {
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
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'
} }

65
demo/index.html Normal file
View File

@ -0,0 +1,65 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p id="out">Push the button</p>
<input id="name"/>
<br/>
<button onclick="get()">Get</button>
<br/>
<button onclick="getTest()">Get Test</button>
<br/>
<button onclick="post()">Post</button>
<br/>
<button onclick="put()">Put</button>
</body>
<script>
const url = "http://localhost:8080/api";
const out = document.getElementById("out");
const name = document.getElementById("name");
const get = async () => {
const res = await fetch(`${url}?name=${name.value}`);
const text = await res.text();
out.innerText = text;
}
const getTest = async () => {
const res = await fetch(`${url}/test`);
const text = await res.text();
out.innerText = text;
}
const post = async () => {
if (!name.value) {
alert("Name is required");
return;
}
const res = await fetch(url, {
method: 'post',
headers: {'Content-Type': 'application/json'},
body: name.value
});
const text = await res.text();
out.innerText = text;
}
const put = async () => {
if (!name.value) {
alert("Name is required");
return;
}
const res = await fetch(`${url}/${name.value}`, {
method: 'put',
headers: {'Content-Type': 'application/json'},
body: name.value
});
const text = await res.text();
out.innerText = text;
}
</script>
</html>

View File

@ -0,0 +1,61 @@
package com.example.demo;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RestController;
@RestController
@RequestMapping("/api/message")
public class ApiController {
private final Logger log = LoggerFactory.getLogger(ApiController.class);
// Список сообщений
private List<MessageDto> messages = new ArrayList<>();
// Получить список сообщений
@GetMapping
public List<MessageDto> getMessages() {
log.info("Get list of messages: {}", messages.size());
return messages;
}
// Получить сообщение по id
@GetMapping("/{id}")
public MessageDto getMessage(@PathVariable(name = "id") int id) {
log.info("Get message: {}", id);
return messages.get(id);
}
// Добавить сообщение
@PostMapping
public MessageDto postMessage(@RequestBody MessageDto message) {
log.info("Add message: {}", message);
messages.add(message);
return message;
}
// Изменить сообщение по id
@PutMapping("/{id}")
public MessageDto putMessage(@PathVariable(name = "id") int id, @RequestBody MessageDto message) {
log.info("Update message {} with new message {}", id, message);
messages.set(id, message);
return message;
}
// Удалить сообщение по id
@DeleteMapping("/{id}")
public MessageDto deleteMessage(@PathVariable(name = "id") int id) {
log.info("Delete message: {}", id);
return messages.remove(id);
}
}

View File

@ -0,0 +1,43 @@
package com.example.demo;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class MessageDto {
private String name;
private String email;
private String text;
private Date date;
public MessageDto() { }
@JsonCreator
public MessageDto(
@JsonProperty(value = "name") String name,
@JsonProperty(value = "email") String email,
@JsonProperty(value = "text") String text,
@JsonProperty(value = "date") Date date) {
this.name = name;
this.email = email;
this.text = text;
this.date = date;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getText() {
return text;
}
public Date getDate() {
return date;
}
}

View File

@ -0,0 +1,15 @@
package com.example.demo;
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");
}
}