Difference between spring @Controller and @RestController annotation

@Controller is used to mark classes as Spring MVC Controller. @RestController is a convenience annotation that does nothing more than adding the @Controller and @ResponseBody annotations (see: Javadoc) So the following two controller definitions should do the same @Controller @ResponseBody public class MyController { } @RestController public class MyRestController { }

‘Field required a bean of type that could not be found.’ error spring restful API using mongodb

Solved it. So by default, all packages that falls under @SpringBootApplication declaration will be scanned. Assuming my main class ExampleApplication that has @SpringBootApplication declaration is declared inside com.example.something, then all components that falls under com.example.something is scanned while com.example.applicant will not be scanned. So, there are two ways to do it based on this question. … Read more

Spring MVC – How to get all request params in a map in Spring controller?

While the other answers are correct it certainly is not the “Spring way” to use the HttpServletRequest object directly. The answer is actually quite simple and what you would expect if you’re familiar with Spring MVC. @RequestMapping(value = {“/search/”, “/search”}, method = RequestMethod.GET) public String search( @RequestParam Map<String,String> allRequestParams, ModelMap model) { return “viewName”; }

How To Inject AuthenticationManager using Java Configuration in a Custom Filter

Override method authenticationManagerBean in WebSecurityConfigurerAdapter to expose the AuthenticationManager built using configure(AuthenticationManagerBuilder) as a Spring bean: For example: @Bean(name = BeanIds.AUTHENTICATION_MANAGER) @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); }

How to create custom methods for use in spring security expression language annotations

None of the mentioned techniques will work anymore. It seems as though Spring has gone through great lengths to prevent users from overriding the SecurityExpressionRoot. EDIT 11/19/14 Setup Spring to use security annotations: <beans … xmlns:sec=”http://www.springframework.org/schema/security” … > … <sec:global-method-security pre-post-annotations=”enabled” /> Create a bean like this: @Component(“mySecurityService”) public class MySecurityService { public boolean hasPermission(String … Read more

download a file from Spring boot rest service

Option 1 using an InputStreamResource Resource implementation for a given InputStream. Should only be used if no other specific Resource implementation is > applicable. In particular, prefer ByteArrayResource or any of the file-based Resource implementations where possible. @RequestMapping(path = “/download”, method = RequestMethod.GET) public ResponseEntity<Resource> download(String param) throws IOException { // … InputStreamResource resource = … Read more

Execute method on startup in Spring

If by “application startup” you mean “application context startup”, then yes, there are many ways to do this, the easiest (for singletons beans, anyway) being to annotate your method with @PostConstruct. Take a look at the link to see the other options, but in summary they are: Methods annotated with @PostConstruct afterPropertiesSet() as defined by … Read more