Java - automatically sets all empty String strings to String

My program extracts data from external sources and installs it in my own POJO classes. Some external data returns null, while others return an empty string. External data has some inconsistency, and therefore I want to standardize this part of the program.

I am creating a web service with a java jersey using Google Gson to convert these POJO classes to json. If String is null, gson will not set the member variable to the json string returned. This will confuse developers who use the web service, as some entries will not have a specific variable. I could write code in each pojo installer to check for null and set it to an empty string. But I will enter the boilerplate code

In eclipse, I tried using a code template to check for a null string in a setter and convert it to an empty string.

if (${param} instanceof String) ${field} = (${param} == null ? "" : ${param}); else ${field} = ${param}; 

However, this will not work if the input type is int.

Is it possible to customize a code template only for inputting a string? Are there other ways to do this automatically?

+5
source share
1 answer

All you need to do is write separate code for input of type int.
Try using some filter condition for querying the type (int and String).

int cannot be passed to String. The priority of strings is greater than int.

-2
source

Source: https://habr.com/ru/post/1234927/


All Articles