Swagger override #JsonProperty("") annotation - spring-boot

In my code I am using #JsonProperty("") for naming fields in JSON response.
Like
#JsonProperty(value = "UserData")private List<UserEntity> userEntities;
And I am getting API response correctly ( that filed name is 'UserData')
but in swagger HTML file the filed name is getting as 'user_data'
In my application.properties following properties are set SNAKE_CASE as default.
spring.jackson.property-naming-strategy=SNAKE_CASE
Could you please tell me how to resolve this. I need the field name as in the #JsonProperty(value = "UserData")

Could you try this?
#ApiModelProperty(name = "UserData")

Have you tried #JsonProperty("UserData")? as per this post

Related

Spring boot model class property validation

I am trying to map a json object to a Spring boot model class now the contract says for a property it have only a certain set of allowed values(not more than 3).
Example:
Suppose that json has field "name" and the contract says allowed values for field "name" are john,todd,phil
Anything other than john,todd,phil wont be accepted.
Is there any way to achive this constraint using any annotations
You can use following solutions
Solution 1:
Using #Pattern annotation with regex , if you want to use case insensitive use appropriate flags
#Pattern(regexp = "john|todd|phil", flags = Pattern.Flag.CASE_INSENSITIVE)
Solution 2:
By creating a enum class type with allowed values
public enum {
JOHN, TODD, PHIL
}
In your model class use #Enumerated(EnumType.STRING) on name filed

How can i hide some attribute conditionally from json response

how can write condition 'if' type is no details other two fields are hide in json response. if possible i want to do it in pojo or bean ?
Using spring boot
spring data rest and hal.
pojo
MongoDB Repository
I want to show accountNo and Accountdetails if type="details"
{
"Name":"Json",
"lastName":"Amazon",
"type":"Details",
"accountNo":"12123",
"AccountdetailsDetails":[ some details]
}
If type="noDetails" just show mentioned response.
{
"Name":"Json",
"lastName":"Amazon",
"type":"NoDetails"
}
I guess you need #JsonFilter.
You can use this annotation to exclude some properties in your entity response.
What you need to do is Add this annotation with unique name in your entity file.
Then serialize this entity values using filter class SimpleFilterProvider.
Take a look on
https://www.logicbig.com/tutorials/misc/jackson/json-filter-annotation.html

Unable to set header as optional in OpenApi using Spring with annotations

I'm using Java 11.0.2, Spring Boot 2.2.6 and Spring OpenApi Core 1.1.49 to create an OpenApi documentation using annotations.
During the request for creating a merchant in the Controller I need to have a custom header property, but which needs to be optional. Based on Swagger documentation for Parameter Object, the field "required" (Determines whether this parameter is mandatory. If the parameter location is "path", this property is REQUIRED and its value MUST be true. Otherwise, the property MAY be included and its default value is false.) by default for header is false, but below you can see that for some reason is true (nevertheless that I configured this option to "false").
Java - part ot Controller method
public ResponseDto create(#Parameter(in = ParameterIn.HEADER, required = false, schema = #Schema(type = "string", format = "uuid"), name = "X-Request-Correlation-Id", #RequestHeader("X-Request-Correlation-Id") #Nullable String headerXRequestId, ... <
This result in OpenApi yaml file - autogenerated with the information from the annotations
parameters:
- name: X-Request-Correlation-Id
in: header
required: true
schema:
type: string
format: uuid
Can you point out the problem, because I can't find a solution in the documentation or anywhere else?!
Found the solution - the problem wasn't in OpenApi annotation #Parameter, but in Spring binding annotation #RequestHeader, which binds the header field to the method parameter. #RequestHeader has also field "required" and by default, it's set on "true" and it overrides the one in #Parameter. So the solution was the following syntax - #RequestHeader(name = "X-Request-Correlation-Id", required = false).

"Missing URI template variable 'teamLeadId' for method parameter of type Long"

I'm trying to get teamLeadId and trying to pass it in the list but it is showing as missing URI template variable! Why this is happening?
#GetMapping(value = "pppp/{id}" , produces =MediaType.APPLICATION_JSON_VALUE)
public List<Team>getUserById(#PathVariable("teamLeadId") Long teamLeadId){
List<Team> team = (List<Team>) teamService.fetchTeamsByTeamLeadId(teamLeadId);
return team;}
To solve the described problem, you should replace pppp/{id} with pppp/{teamLeadId} - the variable id does not occur in your function.
You need to use value = "pppp/{teamLeadId}"
It's looking for that exact variable name in the path you've defined in your GetMapping
Because you take Path param as id.
#GetMapping(value = "/pppp/{id}")
and you try to access teamLeadId
#PathVariable("teamLeadId")
So it not able to find out. You have to use same name in this.
Like,
#PathVariable("id") Long teamLeadId
Why this is happening?
There's a mismatch between the variable name used in the URI template of the #GetMapping annotation (id) and the value indicated in the #PathVariable annotation (teamLeadId). Use one of the following:
#GetMapping("/pppp/{id}")
public List<Team> getUserById(#PathVariable("id") Long teamLeadId) {
...
}
#GetMapping("/pppp/{teamLeadId}")
public List<Team> getUserById(#PathVariable("teamLeadId") Long teamLeadId) {
...
}
Starting on Java 8, if your code is compiled with the -parameters compiler flag and if the method parameter name matches the URI variable name, then you don't need to specify the variable name in the #PathVariable annotation. For example, if the URI variable name is id and the method parameter name is also id, then you can have the following:
#GetMapping("/pppp/{id}")
public List<Team> getUserById(#PathVariable Long id) {
...
}
Refer to the documentation for details.

Renaming APIs in swagger with Spring

I understand that when documenting an API with Swagger in Spring, I can change the description for the API by adding #Api annotation, but when I add it as follows
#Api(value= "NEW_NAME", description="NEW_DESCRIPTION")
Only the description is changed, not the name.
as seen here
Further, I'm not sure where the default name and description are coming from, before adding the API, the name seems to be derived from the controller name, but the description; which to me looks natural and human like almost like hard coded String with capitalization and all.
I ran a search on the code, and I wasn't able to find those Strings. Where's Swagger getting those values from?
thanks
The attribute you are looking for is: tags. So you can avoid grouping by controller name.
From Javadoc of #Api tags:
Tags can be used for logical grouping of operations by resources or any other qualifier.
For example:
#Api(value = "/customers", tags = "customers", description = "Manage Customer")
By default Springfox creates API with name as {controller-name}-controller and description as {Controller Name} Controller (cf. How to change the default Controller Name in Swagger Spring ).
It seems that the current way to do this is:
#Api(description = "Manage cars", tags = { "Cars" })
Default -
{controller-name}-controller
For Custom Name Add -
#Tag(name="YOUR CUSTOM NAME HERE")
on the Controller Class
Example -
#RestController
#Tag(name="1. Project Resource")
public class ProjectResource {...}
Result -

Resources