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();
    }
}

Answers


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?


Need Your Help

how to fixed scroll sidebar (Right/left) like facebook in angularjs

angularjs scroll sidebar

I am new in Angulajs and I have struggle and spend lots of time to build fixed right side directory.

How can I validate the output of XmlSerializer?

c# validation schema serialization xmlserializer

In C# / .NET 2.0, when I serialize an object using XmlSerializer, what's the easiest way to validate the output against an XML schema?