Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RestTemplate does not handle null uri template parameters [SPR-7314] #11972

Closed
spring-projects-issues opened this issue Jun 22, 2010 · 1 comment
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

chris snow opened SPR-7314 and commented

I am trying to call a rest service that has an optional path parameter (areaId, below). Setting the optional path parameter to null causes a NPE (exception below). The implications of this are that I have to create a different version of the method call for each optional call parameter and use lots of conditional logic to check if parameter values are null to decide which method to use.

String FIND_AREAS_QUERY = 
    "/areas/{areaId}?countryId={countryId}&cityId={cityId}"; 

searchCriteria.setCountryID(123);
searchCriteria.setCityID(345);
searchCriteria.setAreaID(null);

areas = restTemplate.getForObject(
    restEndpointUrl + FIND_AREAS_QUERY,
    Areas.class,
    searchCriteria.getAreaID(),   /* NPE */
    searchCriteria.getCountryID(),
    searchCriteria.getCityID()
);

The exception is as follows:

java.lang.NullPointerException
	at org.springframework.web.util.UriTemplate.expand(UriTemplate.java:128)
	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:400)
	at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199)

The current workaround is below: This can get quite a burden for lots of optional parameters.

String FIND_AREAS_QUERY = 
    "/areas/{areaId}?countryId={countryId}&cityId={cityId}"; 

searchCriteria.setCountryID(123);
searchCriteria.setCityID(345);
searchCriteria.setAreaID(null);

if (searchCriteria.getAreaID() != null) {
   areas = restTemplate.getForObject(
       restEndpointUrl + FIND_AREAS_QUERY,
       Areas.class,
       searchCriteria.getAreaID(),
       searchCriteria.getCountryID(),
       searchCriteria.getCityID()
   );
} else {
   areas = restTemplate.getForObject(
       restEndpointUrl + FIND_AREAS_QUERY,
       Areas.class,
       searchCriteria.getCountryID(),
       searchCriteria.getCityID()
   );
}

Affects: 3.0.2

Referenced from: commits 3160ddf

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

Fixed!

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 3.0.4 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants