We will be looking at the request-validator library, which is able to compare the user input against a pre-defined set of rules like required, max, min, email, etc.
In this article, we are going to learn how to configure Spring Boot to handle exceptions, including custom ones, and customize the error responses and handling.
Learn how to seed the database on application startup. Seeders can help you create default admin accounts or other default data required by your application.
"The static methods are hard to mock." - There's nothing to mock in this case. The request-validator library will be used in a service or a controller or even a utility class. What will be mocked is the library's caller and not the library itself. I also think we can all agree one's not expected to mock a controller.
What I mean by "when you want to apply more than one rule to the same field" is imagine a scenario where you need to ensure the email field is required, has a valid email address and is not longer than 200 characters. Each of these validations needs its own annotations on the same field.
However, with the request-validator, it's a matter of specifying the following: "required|email|max:250"
Another advantage of the library is that you can validate a field based on the input of another field in the same request. For example, let's say fieldA can either be COLOURS or PLANETS, then you can add another validation on fieldB such that if the value of fieldA is COLOURS, then fieldB should be present and among a list of COLOURS. This is achieved effortlessly with the library but with javax validation, you'll need to do some gymnastics.
For one, you don't have to create multiple DTOs or litter your code with annotations especially when you want to apply more than one rule to the same field.
In terms of testing, I don't see how it is "bad" for testing in the sense that, if you call the controller with the wrong input, you still get the errors. This applies when calling the controller via MockMvc or a full-blown HTTP request
Currently, there's no way to specify a specific table(s) for backup. I'll suggest you create an issue that'll be labelled feature request or better still a PR.
Though I haven't used Wildfly, my quick look up reveals that you are right. A simple jar file with just what it needs. Spring Boot uses embedded Tomcat by default but can also use embedded Jetty as an application server.
The main advantage and reason why I use this method is to avoid writing SQL. I want to use existing models and ORM instead of writing too much raw SQL plus it's possible to add logic.
I have tried flyway although haven't tried liquidbase.
Summarily, the peculiarity of the technique is the ability to use existing models and repositories to persist data.
Comments
Nov 03, 2022 · Seun Matt
"The static methods are hard to mock." - There's nothing to mock in this case. The request-validator library will be used in a service or a controller or even a utility class. What will be mocked is the library's caller and not the library itself. I also think we can all agree one's not expected to mock a controller.
What I mean by "when you want to apply more than one rule to the same field" is imagine a scenario where you need to ensure the email field is required, has a valid email address and is not longer than 200 characters. Each of these validations needs its own annotations on the same field.
However, with the request-validator, it's a matter of specifying the following: "required|email|max:250"
Another advantage of the library is that you can validate a field based on the input of another field in the same request. For example, let's say fieldA can either be COLOURS or PLANETS, then you can add another validation on fieldB such that if the value of fieldA is COLOURS, then fieldB should be present and among a list of COLOURS. This is achieved effortlessly with the library but with javax validation, you'll need to do some gymnastics.
Hope this helps, cheers.
Nov 02, 2022 · Seun Matt
For one, you don't have to create multiple DTOs or litter your code with annotations especially when you want to apply more than one rule to the same field.
In terms of testing, I don't see how it is "bad" for testing in the sense that, if you call the controller with the wrong input, you still get the errors. This applies when calling the controller via MockMvc or a full-blown HTTP request
May 22, 2019 · Seun Matt
Hi Dhaval,
Currently, there's no way to specify a specific table(s) for backup. I'll suggest you create an issue that'll be labelled feature request or better still a PR.
Cheers.
Mar 04, 2019 · Ganesh Sahu
For SpringBoot 2.0+ This is the equivalent code:
@Bean
public ServletWebServerFactory servletContainer(@Value("${http.port}") int httpPort) {
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setPort(httpPort);
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(connector);
return tomcat;
}
cheers
Jan 28, 2019 · Seun Matt
Glad you can find a way out. However, were you able to get the reason it's unable to create temp dir? File Permission?
Oct 24, 2018 · Rajesh Bhojwani
Great article! I currently used the shared database pattern to handle an application transitioning from monolith towards microservices.
Jul 26, 2017 · Seun Matt
Though I haven't used Wildfly, my quick look up reveals that you are right. A simple jar file with just what it needs. Spring Boot uses embedded Tomcat by default but can also use embedded Jetty as an application server.
Jul 26, 2017 · Seun Matt
That's great to hear!
Jul 24, 2017 · Seun Matt
Jamie thanks for noting that.
The main advantage and reason why I use this method is to avoid writing SQL. I want to use existing models and ORM instead of writing too much raw SQL plus it's possible to add logic.
I have tried flyway although haven't tried liquidbase.
Summarily, the peculiarity of the technique is the ability to use existing models and repositories to persist data.