JerseyTest: how to bind to Set<Integer> - jersey

When testing with JerseyTest, I am having trouble to bind to a:
#Inject
#SomeQualifier
private Set<Integer> aSet;
It works with the following binding:
bind(Collections.EMPTY_SET).to(Set.class).qualifiedBy(new SomeQualifierLiteral());
ONLY when the field is without the generic, e.g. private Set aSet;
How can I make a successful bind if a generic is present on the field definition?

answered by #PaulSamsotha in his comment.

Related

What 'final' keyword next to the field stands for?

In a legacy code, I'm working with, I found the following thing:
#Autowired
final lateinit var controller: CustomController
what does this final keyword mean here?
In a Kotlin documentation I found a short description about final keyword that is blocking overriding of the methods in open classes but no information about fields. Also - the class within which I found the line is not open
A final property or a method in Kotlin prevents overriding of the field / method. That being said, Kotlin by default considers a property or a method/function to be final unless specified by the keyword open. In your case, the final keyword is redundant.
Here's a small demo test case to illustrate the same.
open class Parent {
open val someValue = 0
final val otherValue = 13 // redundant modifier 'final' warning in Android Studio
}
class Child : Parent() {
override val someValue = 5
// override val otherValue = 19 // compile error
}
There is an interesting problem called Fragile Base Class in OOP and why some languages like Kotlin prefer final by default.
What you have there is a property, not a field.
It looks just like a field, as it would in Java; but in Kotlin, it actually defines a public getter method, a public setter method, and a private backing field*.
So the final modifier applies to the accessor methods, preventing those from being overridden in a subclass.  (As you say, the backing field itself can't be overridden anyway.)
As Siddharth says, final is the default in Kotlin, so you usually wouldn't need to specify it, though there are a few situations in which it would be needed — e.g. if it were already overriding something, or you were using the all-open or kotlin-spring compiler plug-ins.  (The use of #Autowired suggests that this is a Spring module, which probably explains why final is needed here.)  In any case, your IDE would probably indicate where it's not needed, e.g. by showing it greyed-out.
(* Only the getter is necessary; the setter isn't generated for a val, and the backing field isn't generated if you override the accessor(s) and they don't refer to it.)

Is it possible to pass a key as default value in #Value annotation of Spring

I have a situation where we are reading one property from properties file and now we have been asked to point to another endpoint and for some time we have to manage both these endpoints unless this new endpoint is tested and validated throughly.
I wanted to handle this situation by adding this newer property in properties file and in the actual class were we are reading this property with #Value Annotation the old one can be passed as default with its key as value something like
#Value("${backend.endpoint:${older.endpoint}}"). is it possible ?
Yes you can do it, I have tested it, my sample code
code:
#Value("#{ ${spring.myapp.usenewval} ? '${spring.myapp.newval}' : '${spring.myapp.oldval}}'}")
private String message;
Properties
spring:
myapp:
usenewval: false
newval: hello
oldval: world.....
You can always set spring.myapp.usenewval from outside like
java -jar -Dspring.myapp.usenewval=true myapp.jar
You can use it like this. (I've personally never done it, so forgive me if I'm wrong)
#Configuration
public class PropertyConfiguration {
#Value("{'${backend.endpoint:${older.endpoint:}}'}")
private String myValue;
}
This #Value annotation uses backend.endpoint, if it is provided and defaults to older.endpoint, if backend.endpoint is not provided.
If neither is provided, the property must be set null.
There are other ways to handle this as well. Probably, use #Value for both the property and handle in code.
Here is quick fix for you. Kindly refer it.
You can set default value to #Value annotation of spring as following.
#Controller
#RequestMapping(value = "/your path")
public class MyController {
#Value("${key:true}")
private boolean booleanWithDefaultValue;
}
Here, I take Boolean variable and set default value as "true".
Hope this solution works.

Spring Custom Configuration not populating properties with underscore

I am trying to populate Custom class with properties. following is my Custom Properties class:
#Configuration
#ConfigurationProperties(prefix = "foo.bar")
public class CustomProperties{
private String PROPERTY_ONE;
private String propertyTwo;
//setters
//getters
}
and my properties in application.properties are:
foo.bar.PROPERTY_ONE=some text
foo.bar.PROPERTY_TWO=some other text
When I am trying to use value from CustomProperties this is what I gets:
customProperties.getPROPERTY_ONE() = null
customProperties.getPopertyTwo() = some other text
So I observed that if I have variable name with underscore(_) in it not populating the property value.
is there any way to get the value with variable having underscore?
Yes, it is 100% possible to get your configuration values.
It's all about the casing! Inside of CustomProperties simply name your first property propertyOne ... and refactor your getters/setters appropriately ... and you'll be good to go!
Spring's got the camel casing going on when translating the configuration fields to your Configuration classes/properties. So instead of matching the casing of your properties, follow the camel casing equivalent of the property name found in your configuration file.
Example: PROPERTY_ONE translates to propertyOne

Reading Environment variable in SpringBootApplication [duplicate]

This question already has answers here:
Spring: How to inject a value to static field?
(5 answers)
Closed 3 years ago.
I have an environment variable RESOURCES_FOLDER. Which i want to read in a class inside my Springboot application
#Value("${RESOURCES_FOLDER}")
private static String resourcesFolder;
When I try printing the value it gives null instead of printing the actual path in my environment variable. Can someone please help me with this ??
Spring does not allow injecting values into static fields. You have a few options to circumvent this restriction:
Create a non-static setter. This is not a particularly good approach, as resourceFolder will be shared by all instances of a class. Nevertheless, you should be able to achieve it by:
public class SomeClass {
...
public static String resourcesFolder;
#Value("${RESOURCES_FOLDER}")
public void setResourcesFolder(String resourcesFolder) {
this.resourcesFolder = resourcesFolder;
}
...
}
Declare the field as non static. For this, ask yourself: do you really, really need the field to be static? Most of the time non-static field is good enough.
Create a separate #ConfigurationProperties class, declare fields as private non-static, create getters/setters for it and inject the class wherever you need the variable. This is a very composable and testable approach, which I would recommend, especially if you have quite a few related properties.
Alternatively, please refer to the other similar questions: 1, 2.
Add your environment variable name in the application.properties file by:
RESOURCE_FOLDER_PATH =${RESOURCES_FOLDER}
Here RESOURCES_FOLDER is the name of your environment variable.
Then access the environment variable in the java class by using #value annotation.
public class AccessEnvironmentVariable{
#Value("${RESOURCE_FOLDER_PATH}")
private String RESOURCE_FOLDER;
private void displayEnvironmentVariable(){
System.out.println("Your environment variable Resource Folder: "+RESOURCE_FOLDER);
}
}

is it possible for custom field mapping using ibatis 2.3? e.g. FACTOR_INFO001..FACTOR_INFO200 map to List<String>

I need a custom mapping using ibatis-sqlmap 2.3.4.726 and spring 2.5.6.SEC01.
class:
public class Rosen {
private String id;
private List<String> factor_info;
}
sql:
select ID, FACTOR_INFO001, FACTOR_INFO002, ..., FACTOR_INFO200
from FACTOR_INFO;
I need to map the FACTOR_INFO* fields to a List<String>. My findings are
1) if I were able to extends/inject RowHandlerCallback then override handleResultObject, it seems possible.
2) if there is something configuration inside the sqlmap xml file.
anybody can guide me or give some hints on how to address it?
thanks
There is an another discussion to answer this question.
Please refer to -> mybatis user discussion

Resources