I need to write a code in which i need to validate the tag and the value of the tag in java, spring boot project:
Request:
{
"recipientType": "individual",
"templateId": "acb",
"to": "9999999999"
}
if I will remove templateId tag from the request itself.
Code should through error description: template id is missing
How can I implement this validation in java spring boot project
You can make use of Bean Validation support in Spring to validate the contents of request body.
Here is a nice tutorial explain bean validation in detail.
https://reflectoring.io/bean-validation-with-spring-boot/
Related
I'm currently struggling to implement a restfull api using
-> Spring Boot 3.0.0, using spring data rest and spring hateoas
-> Spring native support
Basically, I would like to patch an entity, submitting a new value for a field (pointing to an existing entity in Database).
The reference is handled with an hal link. below is the Json object I "PATCH" to my EntityRepository controller:
{
"shouldBeChecked": true,
"name": "Amical6",
"parent": null,
"authority": "https://myurl/api/authorities/e52cdfb6-6f3c-4552-8ea4-e1357b5d052c"
}
Everything is fine when I run the app from my IDE (without GraalVM Native compilation). But when I go live on my test environment (copiled with GraalVM), I get the following errors :
in the web navigator (running the client app):
{"cause":{"cause":null,"message":"Cannot construct instance of `org.[xxx].Authority` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('https://myurl/api/authorities/e52cdfb6-6f3c-4552-8ea4-e1357b5d052c')\n at [Source: UNKNOWN; byte offset: #UNKNOWN] (through reference chain: org.[xxx].MyObject[\"authority\"])"},"message":"Could not read payload"}
And, in my application logs :
Failed to evaluate Jackson deserialization for type [[simple type, class org.springframework.data.rest.webmvc.PersistentEntityResource]]: java.lang.NullPointerException
I'm pretty sure I've got to add some Native Hints related to Jackson (or maybe Spring hateoas ?). Could someone help me with this?
Regards,
EDIT 2022.11.30
Please find below a small reproducer project:
https://github.com/mathieupedreropro/spring-hal-jackson-graalvm
TLDR
I tried to use Spring Hateoas link handling to patch data to my Rest Webservice in a Native compiled Spring boot 3.0.0 application.
It fails at runtime, when the same application works like a charm using a traditional JDK
I am currently working on a Spring Web services(SOAP) project. The requirement is to validate the request payload and return the error response if validation fails and log it. I have extended PayloadValidatingInterceptor to return custom validation message.
As part of logging requirement, we need to print the header values from Request Headers (HttpServletRequest) for tracking purpose.
How to access HttpServletRequest in the implementation of PayloadValidatingInterceptor?
Is it possible to inject HttpServletRequest?
version details:
Spring-Boot : 2.2.6
Spring-ws-core : 3.0.8
Please help.
I tried a springboot web application with freemarker.
In the bootstrap class there's a request-handling method:
#RequestMapping("/showAddPage")
String showAddPage(){
return "showAdd";
}
And i had my template, named "showAdd.ftl" ,lying in the directory of "resources/templates".
I also added freemarker's starter of springboot in pom.xml.
But when i request "localhost:8080/showAddPage", it retured a String, "showAdd", instead of the rendered content of the template "showAdd.ftl".
It doesn't render my showAdd.ftl.
Why could this happen?
I think you must add Servlet Mapping to your DispatcherServlet ;
There is one sample :
https://www.leveluplunch.com/java/tutorials/011-add-servlet-mapping-to-dispatcherservlet-spring-boot/
It would help you
I want to get io.swagger.models.Swagger object in my system, which is a restful backend based on jersey and swagger.
I saw in class ApiListingResource there is such a statement
Swagger swagger = (Swagger) context.getAttribute("swagger");
, which can retrieve the swagger object from servlet context.
Can I do the same in my own code? This seems not a contract that the attribute name will always be "swagger". So I dare not.
Is there any reliable way to retrieve the object?
You can rely on the context (yes, it is set to the name swagger, or with your own logic by extending BeanConfig
I want to validate header parameters that I am going to send while calling REST of spring MVC. The validating class should return JSon object depending on result of validation. how can I do that?