Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ repositories {

dependencies {
implementation("org.springframework.boot:spring-boot-starter")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.projectlombok:lombok:1.18.30")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

вместо implementation ломбок следует подключать через compileOnly

annotationProcessor("org.projectlombok:lombok:1.18.30")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.javaspringcourse.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
@RequestMapping("/headers")
public class HeadersController {

@GetMapping
public String getHeaders(@RequestHeader Map<String, String> headers, Model model) {
model.addAttribute("headers", headers);
return "headers";
}
}
25 changes: 25 additions & 0 deletions src/main/java/org/javaspringcourse/controller/JsonController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.javaspringcourse.controller;

import lombok.RequiredArgsConstructor;
import org.javaspringcourse.exception.BadGatewayException;
import org.javaspringcourse.model.dto.JsonItemIn;
import org.javaspringcourse.model.dto.JsonItemOut;
import org.javaspringcourse.service.JsonService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/json")
public class JsonController {
private final JsonService service;

@PostMapping
public JsonItemOut registerJson(@RequestBody JsonItemIn product) {
return service.register(product);
}

@GetMapping("/error502")
public void getError502() {
throw new BadGatewayException("502 Bad Gateway");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.javaspringcourse.exception;

public class BadGatewayException extends RuntimeException {
public BadGatewayException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.javaspringcourse.exception;

public record ErrorResponse(String code, String message) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.javaspringcourse.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class JsonExceptionHandler {
@ExceptionHandler(BadGatewayException.class)
@ResponseStatus(HttpStatus.BAD_GATEWAY)
public ErrorResponse handleBadGatewayException(BadGatewayException ex) {
return new ErrorResponse("502", ex.getMessage());
}
}
14 changes: 14 additions & 0 deletions src/main/java/org/javaspringcourse/model/JsonItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.javaspringcourse.model;

import lombok.Builder;
import lombok.Data;

import java.util.Date;

@Data
@Builder
public class JsonItem {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Начиная с java 17 для дто следует использовать record, если это возможно

private int id;
private double price;
private Date date;
}
16 changes: 16 additions & 0 deletions src/main/java/org/javaspringcourse/model/dto/JsonItemIn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.javaspringcourse.model.dto;

import lombok.Data;

import java.util.Date;

@Data
public class JsonItemIn {
private double price;
private Info info;

@Data
public static class Info {
Date date;
}
}
20 changes: 20 additions & 0 deletions src/main/java/org/javaspringcourse/model/dto/JsonItemOut.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.javaspringcourse.model.dto;

import lombok.Builder;
import lombok.Data;

import java.util.Date;

@Data
@Builder
public class JsonItemOut {
private double price;
private Info info;

@Data
@Builder
public static class Info {
int id;
Date date;
}
}
35 changes: 35 additions & 0 deletions src/main/java/org/javaspringcourse/service/JsonService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.javaspringcourse.service;

import org.javaspringcourse.model.JsonItem;
import org.javaspringcourse.model.dto.JsonItemIn;
import org.javaspringcourse.model.dto.JsonItemOut;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class JsonService {
private final static List<JsonItem> jsonItems = new ArrayList<>();

public JsonItemOut register(JsonItemIn jsonItem) {
var json = JsonItem.builder()
.id(jsonItems.size() + 1)
.price(jsonItem.getPrice())
.date(jsonItem.getInfo().getDate())
.build();
jsonItems.add(json);
return toJsonItemOut(json);
}

private JsonItemOut toJsonItemOut(JsonItem jsonItem) {
var info = JsonItemOut.Info.builder()
.id(jsonItem.getId())
.date(jsonItem.getDate())
.build();
return JsonItemOut.builder()
.price(jsonItem.getPrice())
.info(info)
.build();
}
}
16 changes: 16 additions & 0 deletions src/main/resources/templates/headers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="ru" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Заголовки запроса</title>
</head>
<body>
<h1>Заголовки запроса</h1>
<table border="1">
<tr th:each="header : ${headers.keySet()}">
<td th:text="${header}"></td>
<td th:text="${headers.get(header)}"></td>
</tr>
</table>
</body>
</html>