This repository is meant for learning and experimenting in Spring Boot and Microservices.
Run the Spring Boot application from RestfulWebServicesApplication.java file. This can be done from the Run-dropdown in IDE.
curl http://localhost:8080/users -u \<user\>:\<password\> | python -m json.tool
[
{
"birthDate": "2021-05-15T16:25:14.953+00:00",
"id": 1,
"name": "pyav"
},
{
"birthDate": "2021-05-15T16:25:14.953+00:00",
"id": 2,
"name": "just-another-user1"
},
{
"birthDate": "2021-05-15T16:25:14.953+00:00",
"id": 3,
"name": "anand"
}
]
After disabling csrf (Cross Site Request Forgery) as given in WebSecurityConfig.java file:
curl http://localhost:8080/users | python -m json.tool
[
{
"birthDate": "2021-05-16T08:52:32.260+00:00",
"id": 1,
"name": "pyav"
},
{
"birthDate": "2021-05-16T08:52:32.260+00:00",
"id": 2,
"name": "just-another-user1"
},
{
"birthDate": "2021-05-16T08:52:32.260+00:00",
"id": 3,
"name": "anand"
}
]
curl -v -d '{"name":"verma", "birthDate":"2021-05-16T13:34:59.485+00:00"}' http://localhost:8080/users -H 'Content-Type: application/json' -X POST
curl http://localhost:8080/users | python -m json.tool
[
{
"birthDate": "2021-05-16T08:52:32.260+00:00",
"id": 1,
"name": "pyav"
},
{
"birthDate": "2021-05-16T08:52:32.260+00:00",
"id": 2,
"name": "just-another-user1"
},
{
"birthDate": "2021-05-16T08:52:32.260+00:00",
"id": 3,
"name": "anand"
},
{
"birthDate": "2021-05-16T13:34:59.485+00:00",
"id": 4,
"name": "verma"
}
]
curl http://localhost:8080/users/1 | python -m json.tool
{
"_links": {
"all-users": {
"href": "http://localhost:8080/users"
}
},
"birthDate": "2021-05-20T16:16:32.407+00:00",
"id": 1,
"name": "pyav"
}
First add the dependency for springdoc-openapi in pom.xml and once it gets downloaded, rerun the application and from the browser, call the url: http://localhost:8080/swagger-ui.html This will redirect to index.html and you would see the swagger UI for the endpoints that you have as part of the application.
The OpenAPI documentation that gets generated and displayed on the swagger ui, can be found in api-docs.json file. This file is created manually and the contents can be found from swagger-ui itself by clicking on the link at "/v3/api-docs".
Following is the actuator url which can be used to monitor health of the application, resources, etc.: http://localhost:8080/actuator
HAL Explorer helps in exploring the APIs in a visualized and way. The url http://localhost:8080/explorer/index.html#uri=/ redirects to http://localhost:8080/explorer/index.html#uri=/
Following is the command to verify one of the ways of versioning in the url:
curl http://localhost:8080/person/produces -H "Accept: application/v2+json" | python3 -m json.tool
{
"name": {
"firstName": "Anand",
"lastName": "pyav"
}
}
- Media type versioning (a.k.a "content negotiation" or "accept header")
- (Custom) headers versioning
- URI versioning
- Request parameter versioning
- URI Pollution
- Misuse of HTTP headers
- Caching
- Browser supportability
- Documentation
To enable basic authentication, add the artifact "spring-boot-starter-security" to pom.xml and comment the csrf disable code in WebSecurityConfig.java. Once the code is built and run, the password will be printed on the build output window. The default user is 'user'.
The url for h2 console is given below. Please also check the pom.xml for relevant artifact.
http://localhost:8080/h2-console
NOTE: If "spring-boot-starter-security" is present in pom.xml, the sql box won't be shown on the browser. Therefore, following line needs to be added in the configure method of WebSecurityConfig class:
http.headers().frameOptions().disable();
The second method is to comment out the security dependency in the pom.xml file, comment out the code in WebSecurityConfig.java file and rerun the project.
From the sql box in the h2-console, we can run any sql command to verify the database entry, for example, "select * from user". This will print the values we have insert from a sql file to the table, say from data.sql file.
The file UserJPAResource.java is added to show the JPA APIs. Here is a sample command and it's output to showcase the h2 in-memory database:
curl http://localhost:8080/jpa/users | python3 -m json.tool
[
{
"id": 1,
"name": "AB",
"birthDate": "2021-06-12T18:30:00.000+00:00"
},
{
"id": 2,
"name": "Jill",
"birthDate": "2021-06-12T18:30:00.000+00:00"
},
{
"id": 3,
"name": "Jack",
"birthDate": "2021-06-12T18:30:00.000+00:00"
}
]
curl http://localhost:8080/jpa/users/1 | python3 -m json.tool
{
"id": 1,
"name": "AB",
"birthDate": "2021-06-12T18:30:00.000+00:00",
"_links": {
"all-users": {
"href": "http://localhost:8080/jpa/users"
}
}
}
After adding the UserRepository interface which is an extension of JpaRepository and using it in UserJPAResource.java file, here is the delete curl call to delete a user:
curl -X DELETE http://localhost:8080/jpa/users/100
curl -v -d '{"name":"pyav", "birthDate":"2021-05-16T13:34:59.485+00:00"}' http://localhost:8080/jpa/users -H 'Content-Type: application/json' -X POST
curl http://localhost:8080/jpa/users | python3 -m json.tool
[
{
"id": 2,
"name": "pyav",
"birthDate": "2021-05-16T13:34:59.485+00:00"
},
{
"id": 3,
"name": "pyav",
"birthDate": "2021-05-16T13:34:59.485+00:00"
},
{
"id": 200,
"name": "Jill",
"birthDate": "2021-06-22T18:30:00.000+00:00"
},
{
"id": 300,
"name": "Jack",
"birthDate": "2021-06-22T18:30:00.000+00:00"
}
]
Here is a demo for POST call which creates a post for a user:
curl http://localhost:8080/jpa/users/200/posts -XPOST -d'{ "description": "My Post New" }' -H 'Content-Type: application/json'
The verification can be done from h2 console by using the following command:
select * from POST where user_id='200'
Exposing SOAP web services in REST style. For example, http://<server>/getPosts
Expose resources with proper URI but improper user of HTTP methods.
Level 1 + proper user of HTTP methods.
Level 2 + HATEOAS i.e. Data + Next possible actions
- Bounded Context - not easy to define boundaries initially and may keep getting changed
- Configuration Management
- Dynamic scale up and scale down - load distribution
- Visibility - how to identify where a bug is, monitoring, etc.
- Pack of Cards - design well, have fault tolerance
Multiple microservices, multiple environments and multiple instances in many of those environments. This means lot of configurations need to be managed.
- Spring Cloud Config Server
- Naming Server (Eureka)
- Ribbon (Client Side Load Balancing)
- Feign (Easier REST Clients)
- Zipkin Distributed Tracing
- Netflix API Gateway
- Hystrix
Please check the project name "spring-cloud-config-server"