0
@Configuration
public class MyWebMvcConfigurationSupport extends WebMvcConfigurationSupport {
    @Override
    public FormattingConversionService mvcConversionService() {
        FormattingConversionService f = super.mvcConversionService();
        f.addFormatter(new DateFormatter("yyyy-MM-dd"));
        return f;
    }
}

@RestController
public class TestController {
    @GetMapping
    public Date test(Date date) {
        return date;
    }
}

When we access http://localhost:8080?date=2021-09-04, the argument type is converted through the DateFormatter's parse method, which relies on the SpringMVC framework to do the conversion. I wonder if the print method can also be invoked through the framework to return a string.

Do we need to manually invoke the print method, for example

@RestController
public class TestController {
    @Resource
    private FormattingConversionService conversionService;
    @GetMapping
    public String test(Date date) {
        return conversionService.convert(date, String.class);
    }
}
andreren
  • 1
  • 1
  • Welcome to SO! Please explain a bit, what you want the controller to handle as input/output formats (MIME types), e.g. `application/xml` or `application/json` or just `text/plain`. – hc_dev Sep 04 '21 at 08:07

2 Answers2

0

Inside the controller

You could use a class extending java.text.Format like SimpleDateFormatin your controller:

@RestController
public class TestController {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 
    @GetMapping
    public String test(Date date) {
        return dateFormat.format(date);
    }
}

At application level

Use DateTimeFormatterRegistrar to register your formats, like described in this tutorial.

Then you can register this set of formatters at Spring's FormattingConversionService.

Using Jackson

However if you would like to work with JSON or XML you should consider using FasterXML's Jackson. See similar question: Spring 3.2 Date time format

hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • This is just an example of formatter interface, not a question of date formatting, print method I understand is formatted as a string, I would like to know whether it can be called by the framework, and if so how to use it – andreren Sep 04 '21 at 08:06
  • @andreren The content-conversion used by Spring depends on _REST's content-negotiation_. This [tutorial](https://www.baeldung.com/spring-httpmessageconverter-rest) explains it. – hc_dev Sep 04 '21 at 08:10
0

This is the interface representing the environment in which the current application is running. It models two key aspects of the application environment: profiles and properties. The methods related to property access are exposed via the PropertyResolver superinterface. A profile is a named, logical group of bean definitions to be registered with the container only if the given profile is active. Beans may be assigned to a profile whether defined in XML or via annotations; see the spring-beans 3.1 schema or the @Profile annotation for syntax details. The role of the Environment object with relation to profiles is in determining which profiles (if any) are currently active, and which profiles (if any) should be active by default.

Properties play an important role in almost all applications, and may originate from a variety of sources: properties files, JVM system properties, system environment variables, JNDI, servlet context parameters, ad-hoc Properties objects, Maps, and so on. The role of the environment object with relation to properties is to provide the user with a convenient service interface for configuring property sources and resolving properties from them.

Beans managed within an ApplicationContext may register to be EnvironmentAware or @Inject the Environment in order to query profile state or resolve properties directly.

In most cases, however, application-level beans should not need to interact with the Environment directly but instead may have to have ${...} property values replaced by a property placeholder configurer such as PropertySourcesPlaceholderConfigurer, which itself is EnvironmentAware and as of Spring 3.1 is registered by default when using context:property-placeholder/.

Configuration of the environment object must be done through the ConfigurableEnvironment interface, returned from all AbstractApplicationContext subclass getEnvironment() methods. See ConfigurableEnvironment Javadoc for usage examples demonstrating manipulation of property sources prior to application context refresh().

Lance Kind
  • 949
  • 12
  • 32