Posts

Showing posts from October, 2021

How to set HTTP response HEADERS in @RestController?

Image
The HTTP response headers are very important. It is so, because the HEADERS values can inform to the HTTP client definitions about how to receive and interpret the received data. It include important fields, such as: ·        SET-COOKIE: ·        CONTENT-TYPE: ·        Content-Language : ·        RESPONSE-CODE: ·        Accept-Language : ·        Access-Control-Allow-Methods : ·        Etc; You can see a list here: https://en.wikipedia.org/wiki/List_of_HTTP_header_fields   Using Spring boot and its @RestController , although there are a few annotations and attributes (ex. @CrossOrigem, produces, etc.) that assists in the definition of how HTTP HEADERS have to be produced, it is possible to generate that with more fine control using the HttpHeaders class. ...

Springboot – Returning HTTP response code based on ResponseEntity

Image
Each http request can have a different response code sent by http server according to the processing result. The response code has to be used properly, according to the semantic defined for each content. There are 5 classes of HTTP responses codes, and they are: ·        1xx: Information ·        2xx: Success ·        3xx: Redirect ·        4xx: Client error ·        5xx Server error   Then, inside each class there are more specific codes, which can inform the client exactly the state of current response. For instance, the error code 404 inform the request resource was not found and the error code 405 inform the request method was not allowed (ex. client makes a request with PUT, but just GET and POST are available). The usage of response code in Spring Boot @RestController is quite simple, and we are going to demonstr...

MapReduce: Functional programming with Node.JS

Image
  In the context of big data, it is often normal we have arrays with thousand entries. And operations to filter, transform (map) or calculate statistics indicators (reduce) had to be often performed. However, without techniques of functional programming, one of few alternatives is to program that using loops, such as for statements, while   statements, or do-while statements. However with map reduce techniques we can focus just in specific functions to be programmed, according with the business rule, and all the complexity to handle big datasets, such as memory consumption and another optimizations are performed by the own technology. The first function we are going to study, is the filter function. Basically it has to receive a function as parameter. This function receives as parameter a current element (it will be executed for each array element) and return a Boolean value. If true the value is maintained in the output value, and if false it will not be presented in th...

Springboot - Connecting into a database (MySQL 8.x) - Repository and EntityManager

Image
  By default springboot is ready to use H2 database. But we can add support to another databases adding their connectors in pom.xml.   The code below show how to include MySQL connector.          <!-- Use MySQL Connector-J -->         < dependency >                     <groupId>mysql</groupId>                     <artifactId>mysql-connector-java</artifactId>        </ dependency      Now we have added  database dependency, we have to update the file application.properties with parameters for database connections. This application.properties if not created yet, can be manually created. The directory for this file location i...