This is a simple realization of custom messages converting in spring-boot HTTP requests and responses.
This is a collection of small and focused instances, each of which covers a single and defined approach to Java application development and an required technologies implementation. This project provides an example of the implementation of centralized message converting of HTTP requests/responses. In this case, the API accepts a DTO with attributes in the Cyrillic alphabet. But the API must and can accept a JSON object with attributes in the Latin alphabet. This example implements a custom incoming/outgoing message converting that simply translates the field names from Latin to Cyrillic and vice versa.
The project with instances of the custom message converting realization.
- For more information on an upcoming development, please read the todo list.
- For more information on a releases, a features and a changes, please read the changelog notes.
These instructions allow to get a copy of this project and run it on a local machine.
Before using it, make sure that follows software are installed on the local machine:
- Oracle JDK 8+ - java development kit;
- Maven 3+ - dependency management;
If any of the listed software is not installed, then it can be installed by instruction as described below.
-
- Install Oracle JDK 11+ according to instructions from an official instruction.
-
- Install Maven 3+ according to instructions from an official source.
In order to install it is quite simple to clone or download this repository.
For the cloning this repository to a local machine, just use the follows link:
https://github.com/innopolis-university-java-team/spring-boot-custom-message-converting-instances.git
To use it is necessary to:
1 - Build the project 2 - Launch the instances 3 - Send http request
To do the full build, execute maven goal package
in the project directory by the following command:
mvn clean package
To do the run example, execute maven goal spring-boot:run
in the project directory by the following command:
mvn spring-boot:run
You can simply import this project in either Eclipse, NetBeanse or IntelliJ IDEA and run it in IDE.
To do the sending requests to the service, just open specifications by address and try it:
http://localhost:8080/swagger-ui.html
This service contains one controller - Request Repeater Controller
. This controller contains four endpoints, such as:
-
fizz-buzz/translated
- allows to send the JSON object with attributes named in both Cyrillic or latin. In this case, the custom message converter will translate request attributes into Cyrillic names. For the evaluating this trick just send one of follows requests and see an APIs logs:1.1.1. Request:
curl -X POST "http://localhost:8080/fizz-buzz/translated" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"buzz\": \"1\", \"fizz\": \"2\"}"
1.1.2. Response body:
{
"fizz": "2",
"buzz": "1"
}
1.1.3. Logs:
21:17:55.000 INFO JSONTMessageConverter : Convert input message into map - {buzz=1, fizz=2}
21:17:55.005 INFO JSONTMessageConverter : Transliterate input message map from EN to RU - {физз=2, бузз=1}
21:17:55.041 INFO JSONTMessageConverter : Convert transliterated input message map into object - TranslatedFizzBuzz(fizz=2, buzz=1)
21:17:55.049 INFO RequestRepeaterController : Получили запрос с транслированными полями ФиззБузз - TranslatedFizzBuzz(fizz=2, buzz=1)
21:17:55.060 INFO JSONTMessageConverter : Convert output message into map - {физз=2, бузз=1}
21:17:55.062 INFO JSONTMessageConverter : Transliterate output message map from EN to RU - {fizz=2, buzz=1}
21:17:55.066 INFO JSONTMessageConverter : Convert transliterated output message map into object - {"fizz":"2","buzz":"1"}
1.2.1. Request:
curl -X POST "http://localhost:8080/fizz-buzz/translated" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"бузз\": \"1\", \"физз\": \"2\"}"
1.1.2. Response body:
{
"fizz": "2",
"buzz": "1"
}
1.1.3. Logs:
21:21:21.176 INFO JSONTMessageConverter : Convert input message into map - {бузз=1, физз=2}
21:21:21.177 INFO JSONTMessageConverter : Transliterate input message map from EN to RU - {физз=2, бузз=1}
21:21:21.178 INFO JSONTMessageConverter : Convert transliterated input message map into object - TranslatedFizzBuzz(fizz=2, buzz=1)
21:21:21.178 INFO RequestRepeaterController : Получили запрос с транслированными полями ФиззБузз - TranslatedFizzBuzz(fizz=2, buzz=1)
21:21:21.179 INFO JSONTMessageConverter : Convert output message into map - {физз=2, бузз=1}
21:21:21.180 INFO JSONTMessageConverter : Transliterate output message map from EN to RU - {fizz=2, buzz=1}
21:21:21.180 INFO JSONTMessageConverter : Convert transliterated output message map into object - {"fizz":"2","buzz":"1"}
-
fizz-buzz/origin
- allows to send the JSON object with attributes named in only Cyrillic. In this case, the custom message converter willn't be used. For the evaluating this trick just send one of follows requests and see an APIs logs:2.1.1. Request:
curl -X POST "http://localhost:8080/fizz-buzz/origin" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"buzz\": \"1\", \"fizz\": \"2\"}"
2.1.2. Response body:
{
"физз": null,
"бузз": null
}
2..1.3. Logs:
21:22:54.925 INFO RequestRepeaterController : Получили запрос с оригинальным FizzBuzz - OriginFizzBuzz(fizz=null, buzz=null)
*BATASH =)
2.2.1. Request:
curl -X POST "http://localhost:8080/fizz-buzz/origin" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"бузз\": \"1\", \"физз\": \"2\"}"
2.1.2. Response body:
{
"физз": "2",
"бузз": "1"
}
2.1.3. Logs:
21:23:49.972 INFO RequestRepeaterController : Получили запрос с оригинальным FizzBuzz - OriginFizzBuzz(fizz=2, buzz=1)
As you can see, the use of such an approach (use custom message converting - the 1 case translated
) allows centralized processing of requests to the API and responses from the API.
- JDK - the java development kit;
- Maven - the dependency management;
- Swagger - documentation and form generator.
- Alexander A. Kropotin - initial work - ololx.
This project is unlicensed - see the lisence document for details.