JsonAlias as default openapi dto property name - java

I have a dto used as request body in a rest service:
#Data
public class MyClass {
#JsonAlias("myAlias")
private String myProperty;
}
When we produce our openapi/swagger yaml the property name is "myProperty"
Is it possible to automatically force the alias inside the openapi/swagger produced?
(annotation? during yaml creation? anything else?)

Serialization always uses the primary name - in this case, myProperty. In order to change the outputted name, you'd need to change the name of myProperty. The JsonAlias annotation only affects deserialization - the alias is an alternate name.

Related

Bind parameters to the sql request inside a #Subselect annotation in Spring boot?

I'm working on a project in which I need to pass parameters to the #subselect annotation of spring boot (which maps a request to an entity), like the following example:
#Entity
#Immutable
#Subselect("SELECT FROM Employe INNER JOIN Employe_adress ON Employe.id = Employe_adress.eid WHERE Employe_adress.aid=?x")
public class Employe {
...
}
I want to bind an external value to "x" variable. Thank you.
Edit: One method I find is adding a global variable, but "The value for annotation attribute must be a constant expression".
It can be done with #FilterDef and #Filter: https://www.baeldung.com/hibernate-dynamic-mapping#parameterized-filtering-with-filter
Defining the #Filter
To demonstrate how #Filter works, let's first add the following filter definition to the Employee entity:
#FilterDef(
name = "incomeLevelFilter",
parameters = #ParamDef(name = "incomeLimit", type = "int")
)
#Filter(
name = "incomeLevelFilter",
condition = "grossIncome > :incomeLimit"
)
public class Employee implements Serializable {
The #FilterDef annotation defines the filter name and a set of its parameters that will participate in the query. The type of the parameter is the name of one of the Hibernate types (Type, UserType or CompositeUserType), in our case, an int.
The #FilterDef annotation may be placed either on the type or on package level. Note that it does not specify the filter condition itself (although we could specify the defaultCondition parameter).
This means that we can define the filter (its name and set of parameters) in one place and then define the conditions for the filter in multiple other places differently.
This can be done with the #Filter annotation. In our case, we put it in the same class for simplicity. The syntax of the condition is a raw SQL with parameter names preceded by colons.

Swagger Example Parameter Value

I want to modify the Example Value under the Data Type on my Swagger-ui interface. currently it contains the following default value (generated by Swagger):
"user":{
"birth":null,
"nationality":null,
"lastname":null,
"firstname":null,
"identity":null
}
I want to specify real values instead of the "null" values.
PS: I use spring boot with annotations: #ApiOperation,...
You must then insert #ApiModelProperty anotation for all your attributes inside your bean. Then put an example attribute inside. Let's say your user is mapped to the User.java class, so you would have:
public class User {
#ApiModelProperty(value = "The birthdate", example = "1985-12-07", format= "yyyy-MM-dd")
private String birth;

Using Jackson XML Mapper, how to serialize more than one properties using same local name

I have an instance of a class that looks as following
public class SomeEntity{
private OpMetric metric = Options.MEASURED;
private Scope scope = Scopes.GLOBAL;
}
Which need to be serialized into following XML
<SomeEntity xmlns="">
<op-metric>
<value>0.3</value>
</op-metric>
<calculated-scope>
<value>updated-global</value>
</calculated-scope>
</SomeEntity >
in both cases the value to be set in the xml is calculated based on enum values of the original fields ,meaning I need to use getters (+ #JsonIgnore on the fields ) and not just annotate the fields.
I've tried to use the following annotation on the getters to generate the format
#JacksonXmlProperty(isAttribute = false, localName = "value")
#JacksonXmlElementWrapper(localName="op-metric")
but it can only be used on one of them due to collision when using the same local name :
com.fasterxml.jackson.databind.JsonMappingException: Conflicting getter definitions for property "value":
Using Mixins did not advance me much since obviously the same limitation applies there as well.
How should I go about creating this XML structure ?
I've ended up creating special methods for the purpose of XML creation ,each of which returns an instance of a class whose only field is named "value", which is then "automatically" gets serialized into the format required .
Annotations were added in using Jackson mixin

ElasticsearchRepository dynamic indexName

In my project I am using ElasticsearchRepository, when my class is extending the ElasticsearchRepository.
Is there a way to change indexName property dynamically, I don't want to use #Document, since my indexName is dynamic?
The #Document(indexName = "xxx") can be a SpEL expression. The expression is parsed whenever the index name is needed by SDE to obtain the index name. So, you could make the name be based on something you configure at runtime, via the application.yml file for example. In theory, anything you can do in SpEL should be possible.
I'm using it to define a "prefix" value that is prependded to all the index names to allow me to run multiple applications with the same base index names in the same ES cluster without clashing with each other. So I have my #Document() tag as:
#Document(indexName = "#{environment.getRequiredProperty('es.prefix') + '-doc'}")
And then I configure the es.prefix to be the value I want in the application.properties or application.yml file.

How to make a property injection via #Value annotation mandatory?

I have the following Test.java POJO class being populated from a property file using the #ConfigurationProperties annotation. I have seen the usage of #Required annotation to make it a mandatory.
Rather than defining annotations at the setter method level, are there any annotations or options within the #Value annotation that I can use for defining conditions like Mandatory, NotNull, etc?
#Component
#ConfigurationProperties(prefix = "com.test")
public class Test {
private String name;
#Required
public void setName(String name) {
name = name;
}
public String getName(String name) {
name = name;
}
}
Is this the right and only way for making a particular attribute mandatory? What are the other such annotations I could use for such conditions or validations purpose?
You can use validation for the properties, just not in the way you envisaged.
What you can do is use standard validation annotations (like #NotNull etc.) on the fields (or setters) themselves.
For example
#NotNull
#Size(min=2, max=10)
private String name;
Check out this part of the documentation
What the documentation essentially says, is that you simply have to have a compatible JSR303 validator implementation on the classpath, and use the relevant annotations.
Spring Boot will take care of the rest
According to the Spring docs (currently 4.1.6.RELEASE), the Value annotation only has a single property, value, containing the value of the property. You can put a Spring EL expression in this, but that won't let you explicitly express notions like non-nullity.
Further, in your code snippet you're using #ConfigurationProperties which is an alternative approach to configuring property values, compared to the #Value annotation.
The way you're doing it, your Java getter/setter names need to map to the property names, i.e. prefix "com.test" + getName() / setName() matches property com.test.name=...
So, you don't need the #Value annotation to tell Spring what property to use.
With the #Value approach, your getters/setters don't have to match the property names, but you do have to annotate each property e.g. #Value("${com.test.name}") and on the class, a #PropertySource annotation pointing to the properties file that contains com.test.name=...
I found a couple of blog posts with code examples that use the 2 different ways to inject the same properties: http://blog.codeleak.pl/2014/09/using-configurationproperties-in-spring.html and http://blog.codeleak.pl/2014/09/testing-mail-code-in-spring-boot.html

Categories