Passing date to request param in Spring MVC

Passing date to request param in Spring MVC

In Spring MVC, you can pass a date as a request parameter by using the @RequestParam annotation and specifying the format in which the date should be sent in the URL. You can also use a ConversionService or @DateTimeFormat annotation to handle date conversion if needed.

Here's how to pass a date as a request parameter:

  1. Using @RequestParam with a Specific Date Format:

    In this approach, you specify the date format in the value attribute of @RequestParam to match the format of the date in the URL.

    import org.springframework.format.annotation.DateTimeFormat; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.Date; @RestController public class DateController { @GetMapping("/date") public String getDate(@RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) { // Use the date parameter in your controller logic return "Received date: " + date; } } 

    In this example, the @RequestParam annotation is used with the @DateTimeFormat annotation to specify the date format pattern, which should match the format of the date in the URL.

    The URL to send a date parameter would look like this: /date?date=2023-11-21.

  2. Using a Custom ConversionService:

    If you need more control over date conversion, you can create a custom ConversionService. First, create a @Configuration class to configure the custom converter:

    import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.converter.Converter; import org.springframework.format.FormatterRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry registry) { registry.addConverter(new StringToDateConverter()); } } class StringToDateConverter implements Converter<String, Date> { @Override public Date convert(String source) { try { return new SimpleDateFormat("yyyy-MM-dd").parse(source); } catch (ParseException e) { throw new IllegalArgumentException("Invalid date format"); } } } 

    Then, use the custom converter by simply using @RequestParam without specifying the date format:

    @RestController public class DateController { @GetMapping("/date") public String getDate(@RequestParam("date") Date date) { // Use the date parameter in your controller logic return "Received date: " + date; } } 

    With this approach, the custom StringToDateConverter will handle the conversion from a String to a Date.

    The URL to send a date parameter would look like this: /date?date=2023-11-21.

Choose the approach that best fits your requirements for handling date parameters in your Spring MVC application.


More Tags

unity-game-engine tnsping promise ms-access-2010 stacked-chart platform-specific windows-server-2016 pausing-execution scrapy sftp

More Java Questions

More Various Measurements Units Calculators

More Genetics Calculators

More Weather Calculators

More Physical chemistry Calculators