why spring-boot application doesn’t require @EnableWebMvc

@SpringBootApplication is a convenience annotation that adds all of the following: @Configuration tags the class as a source of bean definitions for the application context. @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. Normally you would add @EnableWebMvc for a Spring MVC app, but Spring … Read more

Spring MVC: How to use a request-scoped bean inside a spawned thread?

OK, by reading the code in SimpleThreadScope that comes with Spring I think you can create a SimpleInheritableThreadScope by using an InheritableThreadLocal instead. Then just use a bit of xml to register your custom scope: <bean class=”org.springframework.beans.factory.config.CustomScopeConfigurer”> <property name=”scopes”> <map> <entry key=”thread-inherited”> <bean class=”org.mael.spring.context.support.SimpleInheritableThreadScope”/> </entry> </map> </property> </bean> This means that when you create a … Read more

How to use “map.get(key)” in Thymeleaf – Broadleaf Ecom

Using ${map.get(key)} (where key is a variable) works for me. ${map[‘key’]} only seems to work for String literal keys — if the key you’re looking up is a variable then ${map[key]} doesn’t seem to work. Accessing Map entries given a list of keys Here’s a full example, looking up items in a HashMap map given … Read more

optional POST parameter in spring MVC?

If you are using Spring MVC 3.0 or higher then just set defaultValue parameter of @RequestParam: public ModelAndView editItem(@RequestParam(value = “description”, defaultValue = “new value”) String description) In Spring MVC 2.5, I suggest to mark value as required = false and check their value against null manually: public ModelAndView editItem(@RequestParam(value = “description”, required = false) … Read more

Difference between Spring MVC’s @Async, DeferredResult and Callable

Your controller is eventually a function executed by the servlet container (I will assume it is Tomcat) worker thread. Your service flow start with Tomcat and ends with Tomcat. Tomcat gets the request from the client, holds the connection, and eventually returns a response to the client. Your code (controller or servlet) is somewhere in … Read more

Unit testing controllers with CSRF protection enabled in Spring security

The way to solve this issue is : import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*; … @Test public void testLogin() throws Exception { this.mockMvc.perform(post(“/login”) .param(“username”, “…”) .param(“password”, “…”) .with(csrf())) .andExpect(status().isFound()) .andExpect(header().string(“Location”, “redirect-url-on-success-login”)); } The important part is : .with(csrf()) which will add the expected _csrf parameter to the query. The csrf() static method is provided by spring-security-test : <dependency> … Read more

mockMvc – Test Error Message

You can use the method status.reason(). For example: @Test public void loginWithBadCredentials() { this.mockMvc.perform( post(“/rest/login”) .contentType(MediaType.APPLICATION_JSON) .content(“{\”username\”: \”baduser\”, \”password\”: \”invalidPassword\”}”) ) .andDo(MockMvcResultHandlers.print()) .andExpect(status().isUnauthorized()) .andExpect(status().reason(containsString(“Bad credentials”))) .andExpect(unauthenticated()); } MockHttpServletResponse: Status = 401 Error message = Authentication Failed: Bad credentials Content type = null Body = Forwarded URL = null Redirected URL = null Cookies = []

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)