The slides of my talk at Confoo 2014 in Montréal, Qc.
Let's get back to simplicity when doing Java web development. Forget about DI frameworks, ORMs and complex build tools and see how we can develop well-tested, well-crafted web applications using only simple tools, libraries and techniques.
In this talk, I will share my experience rewriting a web application based on Spring MVC, Spring, Hibernate and Maven with only Simple and JDBC. We'll discuss the benefits and challenges of simplicity.
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Simple Web Development in Java
1. Simple Web Development in Java
Vincent Tencé
@testinfected
http://vtence.com
http://github.com/testinfected
2. Déjà vu?
You start a new project by assembling multiple libraries and frameworks
It’s a lot of initial complexity
One of the frameworks keeps getting in the way
The framework feels like a prison
It keeps surprising you with unintended behaviour
3.
4. The Original Version
• Spring with Spring MVC
• Velocity and SiteMesh, UrlRewriteFilter
• Hibernate, JPA, MySQL
• Hibernate Validator
• Maven
• ... 53 jars!
7. The Challenge
• Use simple tools and libraries
• Framework (Web, ORM, DI)
• DIYS (Do It Yourself Simply)
• Ease of assembly, configuration and deployment
• Minimum startup and shutdown costs
• XML
• Annotations
15. Transactions
public class Cashier implements SalesAssistant {
[...]
public OrderNumber placeOrder(PaymentMethod paymentMethod) throws Exception {
[...]
QueryUnitOfWork<OrderNumber> order = new QueryUnitOfWork<OrderNumber>() {
public OrderNumber query() throws Exception {
OrderNumber nextNumber = orderNumberSequence.nextOrderNumber();
final Order order = new Order(nextNumber);
order.addItemsFrom(cart);
order.pay(paymentMethod);
orderBook.record(order);
cart.clear();
return nextNumber;
}
};
return transactor.performQuery(order);
}
[...]
}
16. Validation Constraints
public class CreditCardDetails extends PaymentMethod implements Serializable {
private final CreditCardType cardType;
private final Constraint<String> cardNumber;
private final NotNull<String> cardExpiryDate;
private final Valid<Address> billingAddress;
public CreditCardDetails(CreditCardType type,
String number,
String expiryDate,
Address billingAddress) {
this.cardType = type;
this.cardNumber = Validates.both(notEmpty(number),
correctnessOf(type, number));
this.cardExpiryDate = Validates.notNull(expiryDate);
this.billingAddress = Validates.validityOf(billingAddress);
}
public String getCardNumber() {
return cardNumber.get();
}
[...]
}
17. Validation
public class Validator {
public <T> Set<ConstraintViolation<?>> validate(T target) {
Valid<T> valid = Validates.validityOf(target);
valid.disableRootViolation();
ViolationsReport report = new ViolationsReport();
valid.check(Path.root(target), report);
return report.violations();
}
[...]
}
18. Forms
public class PaymentForm extends Form {
public static PaymentForm parse(Request request) {
return new PaymentForm(new CreditCardDetails(
valueOf(request.parameter("card-type")),
request.parameter("card-number"),
request.parameter("expiry-date"),
new Address(request.parameter("first-name"),
request.parameter("last-name"),
request.parameter("email"))));
}
private final Valid<CreditCardDetails> paymentDetails;
public PaymentForm(CreditCardDetails paymentDetails) {
this.paymentDetails = Validates.validityOf(paymentDetails);
}
public CreditCardType cardType() { return paymentDetails().getCardType(); }
public CreditCardDetails paymentDetails() { return paymentDetails.get(); }
}
19. Thoughts
• Not very “enterprisy”
• What are we missing?
• Use a proven solution or develop our own?
• Not a silver bullet
20. Takeaways
• Avoid the temptation of frameworks
• Use simple tools that do one thing and do it well
• Build your own tools
• Copy the best ideas; write only the simple code you need
• Keep your tools simple and specialized
21. Thanks!
• Simple version :
https://github.com/testinfected/simple-petstore
• Spring version :
https://github.com/testinfected/petstore
• Web :
https://github.com/testinfected/molecule
• Data Mapping :
https://github.com/testinfected/tape