pholtz
pholtz

Reputation: 474

Spring Rest Controller date calculation

Below is a generalized version of my Spring RestController implementation. It's purpose is to respond to HTTP requests at a specific base URI ("/rest/my/uri/to/resource") with a resource based on the URI ID input. It works fine, however due to the structure of the data it returns I have had to add an optional date param. I have the controller calculate today's date and use that in my database query when the user does not supply one in the URI.

My question to you is if I use the todaySqlDate variable for each response where the user does not supply a date as I am below, will it recalculate the date each time it responds to a request? Obviously if the user inputs a date like

/rest/my/uri/to/resource/identifier/666?date=2016-03-15

this will not be an issue. My concern is that when the date is not included, a la

/rest/my/uri/to/resource/identifier/666

it will use the default date. Will the default date stop being current if the service is left running for more than 24 hours?

@RestController
@RequestMapping(value = "/rest/my/uri/to/resource")
public class ResourceController
{

    private static final Logger LOGGER = LoggerFactory.getLogger(ResourceController.class);

    @Autowired
    private ResourceService resourceService;

    public String todaySqlDate = getTodaySqlDate();


    @RequestMapping(value = "/identifier/{id}", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
    public Resource getResource(@PathVariable("id") String id, 
                                @RequestParam(value="date", defaultValue="", required=false) String resourceDate)
                                throws InvalidParameterException, DataNotFoundException, Exception
    {

        LOGGER.trace("In ResourceController.getResouce() with {}", id);

        try 
        {
            if(!isValidIdentifier(id))
            {
                throw new InvalidParameterException("id is not valid: " + id);
            }

            if(resourceDate.isEmpty())
            {
                resourceDate = todaySqlDate;
            }
            else if(!isValidSqlDateString(resourceDate))
            {
                throw new InvalidParameterException("resourceDate is present but not valid: " + resourceDate);
            }

            ResourceList resourceList = resourceService.getResource(id, resourceDate);

            if (resourceList == null)
            {
                LOGGER.trace("No resources found for given input");
                throw new DataNotFoundException("ResourceList for " + id + " not found");
            }

            return resourceList;
        }
        catch (Exception e) 
        {
            LOGGER.error(e.getMessage(), e);
            throw e;
        }

    }

    public String getTodaySqlDate()
    {
        java.util.Date utilDate = new java.util.Date();
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        return sqlDate.toString();
    }
}

Upvotes: 1

Views: 356

Answers (1)

ninja.coder
ninja.coder

Reputation: 9648

Yes, every new request will be handled by a new separate instance (thread) and hence it will re-calculate the date every time.

You can have a look at Spring/REST Documentation for more information.

Useful Link: How are Threads allocated to handle Servlet request?

Upvotes: 1

Related Questions