Cannot convert value of type [org.springframework.web.multipart.commons.CommonsMultipartFile] to required type [java.sql.Blob] - spring

when i used without validation the deatails successfully inserted into DB,But when i applied validation using spring above exception occurr.
java.lang.IllegalStateException:
Cannot convert value of type [org.springframework.web.multipart.commons.CommonsMultipartFile]
to required type [java.sql.Blob] for property 'employee_Uploadphoto'
could anybody please rectify this one?

Related

How to map an input validation error to a specific error code in Spring

I am having a case in which I would like to do some input validation on the #RequestParams of an endpoint.
I know about Validators and Custom Validators, and my current strategy implies creating a wrapper object around the RequestParams, a custom validator and apply class level the annotation that triggers the custom validation.
My problem is that the custom validation is implementing ConstraintValidator, which means that the validator will either return true or false, and an error will be created by Spring with some text (I also know that I can change this text). My desire, however, is to create a custom payload back to the client. An example could be
class MyError {
int code;
String message;
}
The way to return this object is through a #ControllerAdvice Error handler, which understands that a ConstraintValidationException should return my custom payload format. However, I need to return different codes and messages for different reasons on the input validation failed. For example:
A Field is empty -> code XXX
A Field is formatted incorrectly -> code YYY
As far as I know, there is little customization possible on the exception that is reachable from my #ControllerAdvice, I can get a list of errors that happened but I cannot easily determine what happened. (Technically I can, but it would have to be based on the message string, which is pretty weak).
Is there a way to provide extra data to the Exception so I can distinguish from the #ControllerAdvice what happened and create my custom error response accordingly?
Am I approaching it the wrong way?
You can intercept the BindException (https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/validation/BindException.html) with the #ExceptionHandler. This contains detailed information about all validation errors. For example, with e.getFieldErrors() you can access all errors associated with fields.
For example, for a field
#MyConstraint
#Length(min = 3)
private String field;
that failed validation you get the following information in the exception:
Field error in object data on field field: rejected value [XY]; codes [Length.data.field,Length.field,Length.java.lang.String,Length].
Field error in object data on field field: rejected value [XY]; codes [MyConstraint.data.field,MyConstraint.field,MyConstraint.java.lang.String,MyConstraint].
From this you can see that it failed the #Length constraint and the custom #MyConstraint constraint.

Spring Data Mongo: IllegalArgumentException trying to use field reference for minus()

I'm trying to use the $subtract operator to project the difference between two fields as a separate third field using this line of code:
pipeline.add(Aggregation.project("createdTime","modResult").andExpression("createdTime").minus("modResult").as("bucketedTime"));
However, when I try to perform the aggregation, I get the following exception:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: can't serialize class org.springframework.data.mongodb.core.aggregation.Fields$AggregationField
What am I doing wrong here? I've noticed that if I provide an integer instead of a field name, there is no issue. Thanks for your help.
As per my question here, it turns out I should have been using and() instead of andExpression(). The latter tries to interpret the argument as an expression, such as field1 + field2 as a more convenient way of doing .and("field1").plus("field2").

Spring MVC- Joda datetime error message for invalid date

I have a joda datetime attribute in my request object and I have used #DateTimeFormat for the date formatting.
But when I enter an invalid date(say day as 55), it displays the whole message
Failed to convert property value of type java.lang.String to required type org.joda.time.DateTime for property request.myDate; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "55/12/2014" from type java.lang.String to type org.joda.time.DateTime; nested exception is org.joda.time.IllegalFieldValueException: Cannot parse "55/12/2014": Value 55 for dayOfMonth must be in the range [1,31]
How can I change it to a custom message as the present message is generated by Spring.
In another SO question
Spring mvc Joda Datetime converter fail if invalid date
it is asked to make entry in message.properties.
But I'm not able to understand how will it pick the message from property file as I am not doing any error.rejectValue type of thing, So how will the message be identified.
The approach you linked to in your question should work. But the message key you should use is for instance typeMismatch.org.joda.time.DateTime.
Even though you are not manually rejecting the value anywhere Spring will automatically know where to look for the message based on the rules described in the JavaDoc of DefaultMessageCodesResolver.
In the situation you describe Spring will look for following codes specifically:
typeMismatch.request.myDate - Conversion message for attribute named "myDate" on an object named "request"
typeMismatch.myDate - Conversion message for attribute named myDate
typeMismatch.org.joda.time.DateTime - Conversion message for DateTime type
typeMismatch - General conversion error message
So you can define some general typeMismatch message like Incorrect format and then define more specific messages as needed. The more specific error codes have higher priority than those below it. So if you have both typeMismatch and typeMismatch.org.joda.time.DateTime messages defined, the latter will be used.

Spring MVC: Set property to null when bind fails

Is it possible to configure Spring to set the target property to null when binding fails?
For example, my bean has a date property which can have a pre-existing value when the form is displayed. If the user enters an invalid date, I want the property to be set to null; currently it retains its previous value.
I still want the binding error of course, so I can display an error message.
I'm hoping there's a general configuration solution for this (ie. I don't want to just write code to deal with each field manually!).
Background: Retaining the old value causes odd behaviour in subsequent custom cross-field validation. Setting the property values to null would tell this validation not to execute.

Usage of #RequestParam throws error in Spring 3

I am using the #RequestParam annotation to get the request parameters , and using the same to insert the values into the DB.
I have setup the controller to re-direct to the same page which contains the text fields for the user to enter the values, which are being read using the #RequestParam annotation.
But after I enter the values into the text fields , and click submit , it throws this error
Request processing failed; nested exception is java.lang.IllegalArgumentException: Name for argument type [java.lang.String] not available, and parameter name information not found in class file either.
I am a newbie to Spring 3 , and unable to understand the error. Can anybody please shed light on the same.
Thanks in advance,
Vivek
In order to inject the value of request parameter into your handler method parameter, either one of the following should be satisfied
The name of the request parameter must match the name of the method parameter.
e.g.
Following will inject the request parameter named "studentName" into the method parameter studentName
public String goToStep(#RequestParam String studentName)
The request parameter name must be explicitly specified if it does not match the method parameter. The following will inject "nameOfStudent" request parameter into studentName:
public String goToStep(#RequestParam("nameOfStudent") String studentName)
Please post your handler method code if your issue continues to persist.
I asked for the version you are using because I ran with a similar problem a few days ago. I was using Spring 3.1.0.M2 and the same exception appeared when I was using #PathVariable in proxied #Controller.
It was caused by a resolved known bug. You just have to switch to 3.0.6 or try the nightly build 3.1.0.BUILD-SNAPSHOT. Of course, the latter option is not recommended for production environment.

Resources