-
Notifications
You must be signed in to change notification settings - Fork 0
4_Web Service #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kama-Pushka
wants to merge
1
commit into
main
Choose a base branch
from
4task
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/org/javaspringcourse/controller/HeadersController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
25
src/main/java/org/javaspringcourse/controller/JsonController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/org/javaspringcourse/exception/BadGatewayException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
3 changes: 3 additions & 0 deletions
3
src/main/java/org/javaspringcourse/exception/ErrorResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) {} |
16 changes: 16 additions & 0 deletions
16
src/main/java/org/javaspringcourse/exception/JsonExceptionHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
16
src/main/java/org/javaspringcourse/model/dto/JsonItemIn.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
20
src/main/java/org/javaspringcourse/model/dto/JsonItemOut.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
35
src/main/java/org/javaspringcourse/service/JsonService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
вместо implementation ломбок следует подключать через compileOnly