mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
2024-05 spring data rest
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>ru.otus</groupId>
|
||||
<artifactId>spring-31-data-rest</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>spring-31-exercise</module>
|
||||
<module>spring-31-solution</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -0,0 +1,79 @@
|
||||
### "Index page"
|
||||
GET http://localhost:8080/actuator
|
||||
|
||||
### Список бинов, созданных в приложении
|
||||
GET http://localhost:8080/actuator/beans
|
||||
|
||||
### Информация о приложении
|
||||
GET http://localhost:8080/actuator/info
|
||||
|
||||
### Все @ConfigurationProperties
|
||||
GET http://localhost:8080/actuator/configprops
|
||||
|
||||
### Все перепенные окружения
|
||||
GET http://localhost:8080/actuator/env
|
||||
|
||||
### Список логгеров
|
||||
GET http://localhost:8080/actuator/loggers
|
||||
|
||||
### Конфигурация конкретного логгера
|
||||
GET http://localhost:8080/actuator/loggers/org.springframework
|
||||
|
||||
### Изменение уровня логгирования в runtime
|
||||
POST http://localhost:8080/actuator/loggers/org.springframework
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"configuredLevel": "TRACE"
|
||||
}
|
||||
|
||||
### Healthchecks
|
||||
GET http://localhost:8080/actuator/health
|
||||
|
||||
### Собственный healthcheck
|
||||
GET http://localhost:8080/actuator/health/my
|
||||
|
||||
### Список метрик
|
||||
GET http://localhost:8080/actuator/metrics
|
||||
|
||||
### Состояние подключений к БД
|
||||
GET http://localhost:8080/actuator/metrics/hikaricp.connections.usage
|
||||
|
||||
### Загрузка CPU приложением
|
||||
GET http://localhost:8080/actuator/metrics/process.cpu.usage
|
||||
|
||||
### Используемая JVM память
|
||||
GET http://localhost:8080/actuator/metrics/jvm.memory.used
|
||||
|
||||
### Получение данных о запросах
|
||||
# Дополнительно можно настроить SLA, персентили и т.д.
|
||||
# причём для отдельных запросов
|
||||
GET http://localhost:8080/actuator/metrics/http.server.requests
|
||||
|
||||
### А вот все метрики для Promehteus
|
||||
GET http://localhost:8080/actuator/prometheus
|
||||
|
||||
|
||||
### Spring Data REST - Single entity
|
||||
GET http://localhost:8080/person/40
|
||||
|
||||
### Spring Data REST - Collection
|
||||
GET http://localhost:8080/person
|
||||
|
||||
### Add Alex
|
||||
POST http://localhost:8080/person
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"name": "Alex"
|
||||
}
|
||||
|
||||
### Rename Ivan
|
||||
PATCH http://localhost:8080/person/1
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"name": "Anton"
|
||||
}
|
||||
|
||||
###
|
||||
@@ -0,0 +1,61 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>ru.otus</groupId>
|
||||
<artifactId>spring-31-exercise</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.1.3</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<!-- Вот это и есть Spring Data REST -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-rest</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.micrometer</groupId>
|
||||
<artifactId>micrometer-registry-prometheus</artifactId>
|
||||
</dependency>
|
||||
<!-- HAL Explorer - это Swagger для Spring Data REST -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-rest-hal-explorer</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package ru.otus.spring.microservice;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import ru.otus.spring.microservice.domain.Person;
|
||||
import ru.otus.spring.microservice.repostory.PersonRepository;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableWebMvc
|
||||
public class App {
|
||||
|
||||
@Autowired
|
||||
private PersonRepository repository;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(App.class);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
for(int i = 0 ; i < 1000; ++i) {
|
||||
repository.save(new Person("Ivan"));
|
||||
repository.save(new Person("Maria"));
|
||||
}
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package ru.otus.spring.microservice.domain;
|
||||
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package ru.otus.spring.microservice.repostory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
|
||||
import org.springframework.data.rest.core.annotation.RestResource;
|
||||
|
||||
import ru.otus.spring.microservice.domain.Person;
|
||||
|
||||
@RepositoryRestResource(path = "person")
|
||||
public interface PersonRepository extends CrudRepository<Person, Integer> {
|
||||
|
||||
@Override
|
||||
List<Person> findAll();
|
||||
|
||||
@RestResource(path = "names")
|
||||
List<Person> findByName(String name);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>ru.otus</groupId>
|
||||
<artifactId>spring-31-solution</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.1.3</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<!-- Вот это и есть Spring Data REST -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-rest</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.micrometer</groupId>
|
||||
<artifactId>micrometer-registry-prometheus</artifactId>
|
||||
</dependency>
|
||||
<!-- HAL Explorer - это Swagger для Spring Data REST -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-rest-hal-explorer</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package ru.otus.spring.microservice;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import ru.otus.spring.microservice.domain.Person;
|
||||
import ru.otus.spring.microservice.repostory.PersonRepository;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableWebMvc
|
||||
public class App {
|
||||
|
||||
@Autowired
|
||||
private PersonRepository repository;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(App.class);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
for(int i = 0 ; i < 1000; ++i) {
|
||||
repository.save(new Person("Ivan"));
|
||||
repository.save(new Person("Maria"));
|
||||
}
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package ru.otus.spring.microservice.actuators;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Status;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class RandomHealthIndicator implements HealthIndicator {
|
||||
|
||||
private final Random random = new Random();
|
||||
|
||||
@Override
|
||||
public Health health() {
|
||||
boolean serverIsDown = random.nextBoolean();
|
||||
if (serverIsDown) {
|
||||
return Health.down()
|
||||
.status(Status.DOWN)
|
||||
.withDetail("message", "Караул!")
|
||||
.build();
|
||||
} else {
|
||||
return Health.up().withDetail("message", "Ура, товарищи!").build();
|
||||
}
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package ru.otus.spring.microservice.domain;
|
||||
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package ru.otus.spring.microservice.repostory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
|
||||
import org.springframework.data.rest.core.annotation.RestResource;
|
||||
|
||||
import ru.otus.spring.microservice.domain.Person;
|
||||
|
||||
@RepositoryRestResource(path = "person")
|
||||
public interface PersonRepository extends CrudRepository<Person, Integer> {
|
||||
|
||||
@Override
|
||||
List<Person> findAll();
|
||||
|
||||
@RestResource(path = "names", rel = "names")
|
||||
List<Person> findByName(String name);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
include: "*"
|
||||
endpoint:
|
||||
health:
|
||||
show-details: always
|
||||
health:
|
||||
defaults:
|
||||
enabled: true
|
||||
spring:
|
||||
jmx:
|
||||
enabled: true
|
||||
Reference in New Issue
Block a user