Shams
Shams

Reputation: 3667

Handle null values coming in request in Spring request handler

In Spring 3:
My Bean:

public class UserFormBean {

    private String userEmail;
    private String userMobNo;

    public String getUserEmail() {
        return userEmail;
    }
    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }
    public String getUserMobNo() {
        return userMobNo;
    }
    public void setUserMobNo(String userMobNo) {
        this.userMobNo = userMobNo;
    }

}

And my request handler in Controller:

@RequestMapping(value = "/userData", method = RequestMethod.GET)
String userData(UserFormBean bean){
    //code to handle incoming data
}

if user dosen't set any value in 'userEmail' and 'userMobNo' attributes at request time, spring by default sets null in these attributes and when i am getting value of these attributes in my request handler by String str = bean.getUserEmail(); it returns me null in double quote like "null".

Is there any way to convert these null values as "" (blank) when they contain null because i made an extra handler to handle these "null" values. Or some better idea to resolve this issue.

Thanks

Upvotes: 0

Views: 2226

Answers (3)

davorp
davorp

Reputation: 4236

You could define a standard for naming your "web methods", for example:

@RequestMapping(value = "/userData", method = RequestMethod.GET)
String webUserData(UserFormBean bean){
    //code
}

When all your methods start with web*, then you can write an aspect that will initalize all arguments as you would like.

If you need help about creating and defining an aspect, just ask.

UPDATE: creating and defining an aspect

I will not write the code for you, because it's much better for you to learn by reading the documentation - you will get to know a lot of extra details.

That said, the steps to create an aspect are:

  1. Enable AspectJ support - this step is done only once in your whole app
  2. Declare an aspect - create a class that has an @Aspect annotation
  3. Declare a pointcut inside your aspect - specify the point of interest in your code, around which the aspect will do some stuff, in your case, the web* methods
  4. Declare an advice inside your aspect - @Before, @AfterReturning, @AfterThrowing, @After, @Around, here you will write your specific aspect code

In your aspect code you will have to access the current method arguments and do your logic for setting the empty string values for Strings.

Upvotes: 1

DwB
DwB

Reputation: 38300

I suspect the setter is called with "\"null\"" for the new value. Try this:

public void setUserEmail(final String newValue)
{
  if (StringUtils.isNotBlank(newValue) &&
      "\"null\"".equals(newValue))
  {
    userEmail = newValue;
  }
  else
  {
    userEmail = StringUtils.EMPTY;
  }
}

Upvotes: 1

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298898

I think the easiest way would be to initialize the properties to the empty String:

private String userEmail = "";
private String userMobNo = "";

Upvotes: 2

Related Questions