Home > Net >  How to remove backslash from exception message in SpringBoot?
How to remove backslash from exception message in SpringBoot?

Time:07-14

I have this exception message:

 public CityDto getCityByName(String name) throws DataNotFoundException {
    CityEntity cityEntity = cityRepository.findByName(name);
    if (cityEntity == null){
        throw new DataNotFoundException("city with name "   '"'   name   '"'   " not found!");
    }else
        return CityMapper.INSTANCE.toCityDto(cityEntity);
}

and this how Postman show me this message:

{
"status": "NOT_FOUND",
"message": "Entity not found",
"errors": [
    "city with name \"Toronto\" not found!"
]

}

As u can see, city name Toronto for some reason have backslash. How to remove it?

CodePudding user response:

do this throw new DataNotFoundException("city with name '" name "' not found!")

CodePudding user response:

Removing backslash is not the issue, basically you need to understand the technical details why the backslash is there.

For this you can visit this Java Strings W3Schools link to understand as it explains

Because strings must be written within quotes, Java will misunderstand this string, and generate an error:

String txt = "We are the so-called "Vikings" from the north."; 

The solution to avoid this problem, is to use the backslash escape character.

The backslash (\) escape character turns these characters into string characters

The sequence \" inserts a double quote in a string

  • Related