#RequestParam annotated method with swagger ui not showing description - java

I am attempting to mold Swagger-UI in order to modify not only that "name" field but also the "Description" field of an API input parameter. An example is here of someone appearing to do this successfully, utilizing both #ApiParam and #RequestParam: [link]
The closest to modifying the description field I have come so far is the following, where I used the "value" field alone on the #RequestParam input:
However, whenever I try to implement the same structure utilizing both #ApiParam and #RequestParam annotations on a single input element, as shown on the example of the other user above, I get the following error:
Any idea what I'm doing wrong with the annotations here? Is it that this can't be done on a #RequestMapping annotated API?

You are missing a , In between #ApiParam value and name properties
#ApiParam(value="Use GET...", name="schoolId")

Related

Swagger - misleading sample value for date-time field, how can I fix this?

In Swagger model / Example value, I see this sample value for a $date-time field.
"lastModifiedDate": "2020-07-09T12:50:48.461Z"
But I have defined this field like this
#JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", timezone="America/New_York")
private Date lastModifiedDate;
So the actual values returned by my API look like this:
"lastModifiedDate": "2020-07-09T07:44:35.366-04:00"
So I am not sure why swagger is not detecting this.
Probably because this JsonFormat is a Jackson annotation.
com.fasterxml.jackson.annotation.JsonFormat
So... do I need to add some additional Swagger annotation here?
I don't have a Swagger descriptor file (or don't have control over it),
I have just annotations in the Java code.
Can this be done via annotations?
I think that the annotation required for documenting your moddel is #ApiModelProperty, take a look on it http://docs.swagger.io/swagger-core/current/apidocs/io/swagger/annotations/ApiModel.html
hope it will be helpfull.

include_in_all setting in spring data elasticsearch

According to Elasticsearch documentation it is possible to exclude a field from _all field using include_in_all setting (set to false). I need to exclude a field from _all and I'm using spring data elasticsearch do define my mappings. I haven't found a way to do it this way.
Is this possible using spring data elasticsearch annotations?
Unfortunately, Spring Data Elasticsearch cannot do this thing. The improvement request is already a year old, but its priority is minor, so there is no hope for now:
https://jira.spring.io/browse/DATAES-226
In my last project I had to do a lot of simple searches (instead of one by "_all" fields, I used 1 search per each field), and then I united all the results. I assume that there is no nice solve for that problem by Spring Data Elasticsearch.
You can save the mapping of your type into a json file. Then you add the '"include_in_all": false' to the field you want to exclude. This should look something like this.
{
"my_type": {
"properties": {
"excludedField": {
"type": "text",
"include_in_all": false
}
}
}
}
Then you load the file using your favorite JSON reader, parse it as a string and change your mapping with the elasticsearch api .
client.admin().indices()
.preparePutMapping("my_index")
.setType("my_type")
.setSource(loadedFileString)
.get();
EDIT: I just noticed you wanted to use annotations for it. Maybe the #Field annotation has a parameter for it? I'm sorry, I have no experience with the annotations.

How to create a custom validator annotation in Spring Boot?

I would like to validate both the #RequestBody as well as path parameters. Ideally, I'd like to create my own #CustomValid annotation instead of the #Valid one.
In addition to doing checks like #NotNull on the DTO itself, I want to actually check if say, the id of the object to be updated actually exists in the database. For example for this request:
{
"id": "2348291983918",
"name": "Carol"
}
I'd like to do a lookup for whether the users mongo collection actually contains an object with the id passed in or reject it.
Does anyone know how to do this?

Swagger Example Post Body From Annotations

Does anyone know if it's possible to create an example post body with pre-populated/default values from Java annotations? My goal is for users to have a working example when viewing a POST endpoint in Swagger UI. Ideally this working example is created from annotations in the code.
For Example on a model object property:
#ApiModelProperty(example = "http://istock.com/my_cool_image")
#JsonProperty("submitted-image-url")
private String submittedImageUrl;
Would produce something like this in Swagger UI (note the example URL shows up in the Model Schema):
The way it appears to be designed, you have to click on Example Value under Data Type for the request/Value textarea to be populated (at least in Swagger 1.5.9).

Spring "typemismatch" and required fields

In the context of Spring Webflow 2.0.x......
I handle form binding "typemismatches", i.e. as a result of trying to map a String onto a Integer field, by using the following in my messages.properties
typeMismatch={0} contains invalid data.
This works fine.
The problem is that if the field that the typeMismatch error occurred on was "required" then I also receive an error for the missing required field, which is logical I guess because the value that was submitted was never bound. ("Required" being defined in a Commons Validation XML file)
So, I dont want to see the "XXX is required field" error message when the field is only missing due to the typeMismatch. How do I resolve this? I thought about overriding initBinder() on the FormAction but quickly got nowhere.....
Like Yves mentioned, among the three approaches, i have used a custom validator method and its very easy. You can use a custom validator which checks if the form field already has a xml error message of required. If the field does not have an error, then you can check for your string validation. That way it will display only one.
The other method that you could use is try a multiple xml validation, one being required and the other one being a mask which checks for a particular regular expression. In your case if your field is an integer field, then you can go and perform a mask with regex checking for only numbers. The order of mask, required or required, mask in the xml decides which message gets a higher preference.
For example:
<field property="somefield" depends="required,mask" page="2">
<arg key="somelabel"/>
<var>
<var-name>mask</var-name>
<var-value>${somepattern}</var-value>
</var>
</field>
You have many options, in order of preference:
Set selectively the message typeMismatch.target.yourFieldName or typeMismatch.int in resources files
Implement your own Validator so that you can send a dedicated message when Integer parsing will fail before the binding step
Create a BindingErrorProcessor to handle different kind of parsing issues

Categories

Resources