Looking into documentation regarding #Valid annotation I can't figure out one thing.
Is there any difference between following:
#Valid
List<SomeClass> foo;
List<#Valid SomeClass> foo;
Thx in advance
Related
Currently, I am learning Spring Boot. I got trouble with accessing the #Transient variable of Entity class from my #Service class.
Actually, I want to calculate the popularity of the book in my Service and want to assign it to a specific book.
#Transient
private Double popularity;
Any tips on solving this problem.
Let me know if you need more information.
Thank you in advance!
Transient properties are properties that do not have an appropriate column in the database table. But of course, they should have getters and setters, as any other property.
So after you calculate the popularity, just simply use the setter (setPopularity) as for any other class property.
I'm really struggling to combine #Valid with #RequestHeader.
Might this be not supported or is there a way to enable it? I couldn't find useful information about that...
When I annotate the whole controller with #Validated it works, so it is not a big issue. However, I feel like it should work with #Valid as well, so I wanted to know if I'm missing something here.
Code example:
#GetMapping("/validationControllerHeader")
public String validationControllerHeader(#Valid #RequestHeader #Pattern(regexp = "[a-z]{3}[0-9]+") String someheader) {
return someheader;
}
I'm using #Valid in the same test controller for query parameters and body validation too and there it works, so the issue is only present with headers.
Using spring boot 2.3.1.RELEASE
You are definitely supposed to use #Validated in your controller class, as it indicates that the validation is meant to be performed in that class. From the documentation:
To be eligible for Spring-driven method validation, all target classes need to be annotated with Spring’s #Validated annotation, which can optionally also declare the validation groups to use.
And, as you are using #Pattern (which is a Bean Validation annotation), you don't need #Valid.
I started with smaller projects to learn how to use Spring. Actually I have a big problem.
Code Redundacy
Maybe I missunderstood something but I have #Entity Classes to describe how my SQL Tables/Structure must be. On the otherside I have serializable classes. Example:
#Entity class UserEntity
and
class User implements Serializable
On CRUD operations I must transfer Values between this two. But why? This two classes are like the same for me. They have same members and getters/setters. Is there an elegant way to avoid this redundacy?
Maybe I do it completley wrong?
I would assume that your question is "Why do I need to make DTO for Entities? Isn't that redutant?"
Simple answer : Safety reason.
Complex answer :
So there are some risk with JSP and MVC where if you put your managed entities into frontend, there are posibilities where you can inject data into database. Which is bad for site of course : )
For more detail information check https://o2platform.files.wordpress.com/2011/07/ounce_springframework_vulnerabilities.pdf
With reference to my previous linked in question I'm bit confused with the usability of #JsonAutoDetect.
I solved the problem by adding #Getter to FieldValues class and removed the #JsonAutoDetect.
So now it let me thinking, what would be the scenario where #JsonAutoDetect can be used, as I can achieve the same result without having it. What is the purpose of having #JsonAutoDetact annotation over having getter methods. Am I missing something.
Not able to write any comment for previous question so created a new one.
Here is an article that I think can help you. The url is https://www.baeldung.com/jackson-jsonmappingexception .
At my point, if you use jackson-databind jar, spring underlying use ObjectMapper to serialize JavaBean. If neither javaBean's field nor getter method is public, spring could not serialize JavaBean automaticlly. Annotation #JsonAutoDetect is used to custom your javaBean, by which way you can set field limits to any level (e.g. protected public private... so that you can serialize the javaBean successfully).
If I don't understand wrong, the #Getter is from lombok that automaticlly help you generate public getter method.
Disclaimer: New to Kotlin, may be a easy to solve issue or misunderstood basics.
I am trying to inject a List of a specific interface's Spring implementations, in a regular java class this have been easy like this:
#Autowired
List<IMyClass> myClassList;
But in Kotlin doing the following gives me a error
#Autowired
lateinit private var myClassList: List<IMyClass<Any>>
// No beans of '? extends IMyClass<Object>' or 'List<? extends IMyClass<Object>>' types found
Doing it like this:
#Autowired
lateinit private var myClassList: List<IMyClass<*>>
Makes the injection work but doesn't allow me to use a function defined in the interface that takes a generic object as input
// Out-projected type 'IMyClass<*>' prohibits the use of 'public abstract fun myFunction(obj: T!): T! defined in com.path.IMyClass'
So how am I supposed to solve this? Is it easier to rewrite the interface in Kotlin with some specific syntac?
Thing you're doing in Java is just using implicit wildcard. So you have 2 ways here:
Try to refactor API and inject list of List<IMyClass<? extends SomeInterface>>
Use injected List<IMyClass<*>> but cast it explicitly to thing you need, i.e. myClassList as List<IMyClass<Any>>
Thing here is kotlin's erasure is more explicit. If you don't know what type is there — we can't guarantee your code will work, because there is such type in kotlin as Nothing, instance of which can't exist.
I faced a similar situation and as answered by asm0dey, casting was solution which I felt was better for me, but doing it in a cleaner way was a concern.
Just answering below how exactly I did it in the best possible way that I could think of:
#Service
class MyService(_myClassList: List<IMyClass<out Any>>) {
#Suppress("UNCHECKED_CAST")
val myClassList: List<IMyClass<Any>> = _myClassList.map { it as IMyClass<Any> }
// ...
}