How to define the default value for #QueryParam? - java

When I add the default value of the string in JAX-RS, it doesn't take the value. It stays to null or empty.
#QueryParam("status")
private String status = "confirmed";
When I pass the status as empty or null or undefined it stays as empty or null or undefined. It doesn't take the default as confirmed.

Use the #DefaultValue annotation to specify the default value of the request meta-data that is bound to #PathParam, #QueryParam, #MatrixParam, #CookieParam, #FormParam and #HeaderParam annotations:
#QueryParam("status")
#DefaultValue("confirmed")
private String status;
If a method parameter, resource class field, or resource class bean property is not annotated with #DefaultValue and the corresponding meta-data is not present in the request, the value will be:
An empty collection for List, Set or SortedSet.
null for other object types;
Java-defined default for primitive types.

Related

Accessing optional parameters in Spring Boot GraphQL

I have this QueryMapping method where I have some parameters marked as required in my GrapthQL schema, but not all. Using #Argument allows me to grab all required parameters, but when I send a Query without an optional parameter it crashes. Using the RequestParam annotation with a default value doesn't work since its type is an integer and the annotation requires a string. (I guess it's supposed to be called within a REST-API)
#QueryMapping
public List<Record> getRecord(Argument String email, #Argument int dateFrom, #RequestParam(required = false, defaultValue = 0) int dateTo) {
return repository.findSpecific(email, dateFrom);
}
Edit: Method overloading does not work.
What can I do?
I found a solution by using Kotlin: Adding a Question mark behind the Parameter Type allows me to set the value to null.

Need to include #Json non-null in swagger code gen

I need to add #Json non-null in call level but I am not able to do it from swagger code gen.
Hence, could you please help me with this issue?
you can use the #io.swagger.v3.oas.annotations.media.Schema annotation on your method parameters. This annotation allows you to specify the data type, format, and other properties of the parameter, and you can use it to specify that the parameter is required (non-null) using the required property.
Here is how you might use the #Schema annotation to specify that a method parameter is required:
#GET
#Path("/users/{id}")
public User getUser(
#PathParam("id") #Schema(required = true) String userId
) {
...
}
In this example, the #Schema annotation is used on the userId parameter to specify that it is required. This will ensure that the generated code includes the #Json non-null annotation on the parameter, which will enforce the requirement at runtime.
You can also use the #Schema annotation to specify other properties of the parameter, such as its data type, format, and description. For more information, you can refer to the Swagger Code Gen documentation.

How to validate string field in a rest request against enum value?

I am implementing a controller class in a spring-boot project and want to validate retrieved model of post method. It has a string field and should be validated against an enum value.
I wonder is there any validation annotation which will get enum class and check if value has a valid enum value? For example :
class ModelObject{
#EnumValidator(MyEnumClass.class)
String inputField;
}
If you simply specify the Enum as your #RequestParam, it will validate against the values present in enum.
Additionally, if you are using swagger-ui, this is bound to the values of the enum field.

Can you have an optional QueryParam in jersey?

Using java jersey, I have the following #QueryParam's in my method handler:
#Path("/hello")
handleTestRequest(#QueryParam String name, #QueryParam Integer age)
I know if I do:
http://myaddress/hello?name=something
It will go into that method....
I want to make it so that I can call:
http://myaddress/hello?name=something
And it will also go into that same method. Is there any way I can flag an "optional" PathParam? Does it work with #FormParam too? Or am I required to create a separate method with a different method signature?
In JAX-RS parameters are not mandatory, so if you do not supply an age value, it will be NULL, and your method will still be called.
You can also use #DefaultValue to provide a default age value when it's not present.
The #PathParam parameter and the other parameter-based annotations, #MatrixParam, #HeaderParam, #CookieParam, and #FormParam obey the same rules as #QueryParam.
Reference
You should be able to add the #DefaultValue annotation the age parameter, so that if age isn't supplied, the default value will be used.
#Path("/hello")
handleTestRequest(
#QueryParam("name") String name,
#DefaultValue("-1") #QueryParam("age") Integer age)
According to the Javadocs for #DefaultValue, it should work on all *Param annotations.
Defines the default value of request meta-data that is bound using one of the following annotations: PathParam, QueryParam, MatrixParam, CookieParam, FormParam, or HeaderParam. The default value is used if the corresponding meta-data is not present in the request.
You can always wrap return type in optional, for example: #QueryParam("from") Optional<String> from

Spring MVC default value not working

#RequestMapping(value = "/Fin_AddBankAccount", method = RequestMethod.POST)
public #ResponseBody JsonResponse addCoaCategory(
#RequestParam(value="code", required=true) long code,
#RequestParam(value="startFrom", required=true) long startFrom,
#RequestParam(value="name", required=true, defaultValue="N/A") String name)
{
}
defaultValue="N/A" not working , As I did not provide any text in name field , it store null in database instead of "N/A"?
What is the point of setting a default value if you really want that parameter.
if you mark it as required true(not needed as it is default) then no need of a default value.
If that parameter is not mandatory then mark it as false and give a default value.
Documentation of Spring RequestParam.required
Default is true, leading to an exception thrown in case of the parameter missing in the request. Switch this to false if you prefer a null in case of the parameter missing.
From your question I figured out that you are sending parameter name with empty value using POST request. According to the Spring documentation you should not send name parameter in the request in order to use default value. Simply remove name field from HTML form if it is empty.
It seems that default values makes more sense for GET requests.
make sure you don't pass empty string value
Valid Methods:
1. Fin_AddBankAccount?name=
O/P: name="N/A"
Fin_AddBankAccount?
O/P: name="N/A"
Invalid Methods:
Fin_AddBankAccount?name=""
this will set empty string to variable i.e. name="";
In my project
#RequestParam(value="name", required=true, defaultValue="N/A") String name
This code correctly sets name variable as defaultvalue N/A when requestparam "name" was not provided. My guess is you are not inserting this name variable into the table properly so database is storing null instead of "N/A". Please show us or double check the data access object code. Good luck
Thanks #TiarĂª Balbi, in fact you do not need "required=true" because defaultValue="N/A" implicitly sets this variable as required=false anyways.

Categories

Resources