5 ms·
Spring Boot is still Spring with its annotation madness. Take parameter validation, for example. Crazy, full-screen-width multiple annotations stuffed inside th
by cutler 4y ago
Spring Boot is still Spring with its annotation madness. Take parameter validation, for example. Crazy, full-screen-width multiple annotations stuffed inside the method parameter list. What on earth is wrong with doing validation as other frameworks do, ie. in the method body?
- geodel 4y agoExactly. I feel calling SpringBoot as polished turd would be wrong. It's not even polished, just turd. Where ever I have been forced to use it is 4-5 times the amount of code that would normally be needed for such functionality. Large number of bloated Spring libraries and converts normal compile time errors to runtime exceptions. But tools at VMWare are relentless in pushing it through enterprises.
- oflannabhra 4y agoYes, thank you. Spring Boot is annotation hell. Annotations are the worst part of Java—it’s what happens when a language can’t support a true DSL. Just as ridiculous is the HttpSecurity method chaining “DSL”.
- al_mandi 4y agoJava annotations can support compile time validation (e.g. see micronaut or checker framework). DSLs don't.
- Banana699 4y ago> DSLs don't. DSL is a general name to any embedded language inside a host language. C++'s templates can be used to write DSLs, they are compile-time checked. Kotlin's lambda syntax can also create very convincing DSLs[1] to describe hierarchical structures\processes, and they are fully typed, compile-time checked constructs. There are far more to DSLs than hacky strings (regex) or chaining method calls at runtime (fluent APIs). It's a very general pattern. [1] Example from https://kotlinlang.org/docs/type-safe-builders.html https://kotlinlang.org/docs/type-safe-builders.html html { head { title {+"XML encoding with Kotlin"} } body { h1 {+"XML encoding with Kotlin"} p {+"this format can be used as an alternative markup to XML"} } }
- al_mandi 4y agoWhy write many lines of validation when one line can suffice? @Entity class User { @NotBlank String username; } // Use site public User addUser(@Valid User user) { ... } as opposed to class User { String username; public void validate() throws ValidationException { if (username.isBlank()) throw new ValidationException("username is empty"); } } // Use site public User addUser(User user) { user.validate(); // ... }
- cutler 4y agoI was thinking more about @RequestParam validation eg. fun calc1(@RequestParam @NotNull @Size(max=10) @Pattern(regexp = "^\\d{4}-\\d{1,2}-\\d{1,2}$") date:String, @RequestParam @NotNull @Size(max=5) @Pattern(regexp = "^\\d{2}:\\d{2}$") time:String, @RequestParam @NotNull @Size(max=40) @Pattern(regexp = "^[-_'A-Za-z/\\d]{2,50}$") zone:String, ): Map<String, Any> { Why stuff everything in the param list? No other framework I can think of proliferates annotations like this. Spring annotations being Java-based means I must suffer Java's ridiculous inability to handle regex metacharacters - even after introducing raw string literals in Java 13. Kotlin handles this perfectly but once I'm in Spring annotation-land Spring's touted Kotlin compatibility goes out the window.
- al_mandi 4y agoSure, but nothing is stopping you from writing a `validate()` method right? It also appears to me that having so many validations for a base type `String` is indicative that there should be a `Date`, `Time`, and `Zone` type respectively for each of the arguments.
- mesarvagya 4y agoCode like this is expected in Spring https://hastebin.com/jevibugiqu.less https://hastebin.com/jevibugiqu.less . Literally all the classes I see are filled with annotations. Can we even write code in pure java without a single annotation ? Theres a lot of magic going on with annotation. It works great while it is running. God forbid there is some issue and we have to figure it out where all the magic is happening.
- bluefirebrand 4y agoDoing validation in the method body scales terribly, is hard to maintain, isn't very testable, etc. However, defining the validation schema in the parameter list is ugly too. Define it elsewhere and have it run through a validation middleware.