Swagger Example Parameter Value - java

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;

Related

JsonAlias as default openapi dto property name

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.

REST API java class set initial value of setter getter if no data in database

i have a rest api, i get the data using DAO, mapping it from class to another DTO class, if no data present in database, instead of returning null like this
{
"tes":null,
"tez": null,
"example": null
}
i want to return like this:
{
"tes":"",
"tez": "",
"example": ""
}
i have more than 100 field. so i think its not good to set value in setter getter in all field. how to do this is simple way?
As I see you have two options.
Instead of manualy mapping from DAO to DTO Class you need to use a ModelMapper Class with
modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());
As your problem is not clearly the mapping but generating JSON from your DTO, you can use Jackson
#JsonInclude(Include.NON_NULL)
This will skip the empty/null values in your generated JSON.
See: Spring REST Service: how to configure to remove null objects in json response
You could use Lombok like so
#Builder.Default
private String tes = "my-default-value";

Swagger + Spring Boot, how to set the example value to be passed in the API based on Spring Boot Validation

In all of my API calls where I have used DTOs to pass the information, in Example values, I see all the fields of the DTO and I have applied Validation groups in my Spring Boot application on DTOs like,
#NotNull(message = "error_card_expiry_month_required", groups = AddUpdateCard.class)
#JsonProperty("month")
private Integer Month;
#NotNull(message = "error_card_expiry_year_required", groups = AddUpdateCard.class)
#JsonProperty("year")
private Integer Year;
#NotBlank(message = "error_card_cvv_required",groups = AddUpdateCard.class)
#JsonProperty("cvv")
private String Cvv;
here I have used groups = Class.class for validation.
How can I tell swagger, If possible, to use this so that it doesn't show entire DTOs as example value and only show me the values that I have validated?
I just want the Validated #NotNull annotated values for that API only.
Is it possible or will I have to use any other way to do this?
Try adding the following code to every field.
#ApiModelProperty(name = "The chosen month", position = 0, example = "6")
#NotNull(message = "error_card_expiry_month_required", groups = AddUpdateCard.class)
#JsonProperty("month")
private Integer Month;
#ApiModelProperty is used by swagger to create DTO (requestbody) for the given property.
name: The name that should be shown by swagger as the information tag in the model
position: The order in the requestbody that this property has
example: A pre-filled example in the requestbody field in swagger
It might not be able to sort through valid examples based on bean validation, but since you know your own constraints you can pick whatever example value you want.

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

SpringData Mongo #Column equivalent annotation (#Property?)

Is there a SpringData Mongo equivalent of the JPA #Column annotation?
Basically, I've got a POJO with a property that I want to store in Mongo with a different name. So, the following object:
public class Pojo{
#Property("bar")
private String foo = "Hello World";
}
would be persisted as:
{
"_class":"com.example.Pojo",
"bar" : "Hello World"
}
Note: I don't want to use the MappingMongoConverter to explicitly do it
The Spring Data reference documentation lists #Field as the annotation to customize the key and ordering of properties mapped to a MongoDB document.
You can use #Field
It lets you define custom key name in DB and the insert order

Categories