I want to enable/disable a @RestController
based on configuration, in order achieve it i'm trying to use the @ConditionalOnExpression
annotation.
Using a static hardcoded value is working just fine:
@RestController
@ConditionalOnExpression("true")
public class MyRestController {
@RequestMapping("/hi")
public String hi() {
return "hi";
}
}
Yet, using a dynamic property value within a SpEL expression always results in not loading the @RestController
:
@RestController
@ConditionalOnExpression("${my.rest.controller.enabled:false}")
public class MyRestController { ... }
Would really appreciate any ideas/best practices to resolve this.
In simple words, @RestController is @Controller + @ResponseBody. So if you are using latest spring version you can directly replace @Controller with @RestController without any issue.
Spring RestController annotation is a convenience annotation that is itself annotated with @Controller and @ResponseBody . This annotation is applied to a class to mark it as a request handler. Spring RestController annotation is used to create RESTful web services using Spring MVC.
RestController: RestController is used for making restful web services with the help of the @RestController annotation. This annotation is used at the class level and allows the class to handle the requests made by the client.
Spring 4.0 introduced the @RestController annotation in order to simplify the creation of RESTful web services. It's a convenient annotation that combines @Controller and @ResponseBody, which eliminates the need to annotate every request handling method of the controller class with the @ResponseBody annotation.
To verify that a property in Spring Cloud Config is not provided, I used the following expression:
/**
* @return The Jms Template for sending the messages to the queue.
*/
@Bean(name = "jmsTemplate")
@ConditionalOnExpression("!'${jsk.messaging.jms.queue}'.isEmpty()")
public JmsTemplate jmsTemplateForQueues() {
JmsTemplate jmsTemplate = new JmsTemplate();
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(activeMQConnectionFactory());
jmsTemplate.setConnectionFactory(cachingConnectionFactory);
return jmsTemplate;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With