Serving static web resources in Spring Boot & Spring Security application

There are a couple of things to be aware of: The Ant matchers match against the request path and not the path of the resource on the filesystem. Resources placed in src/main/resources/public will be served from the root of your application. For example src/main/resources/public/hello.jpg would be served from http://localhost:8080/hello.jpg This is why your current matcher … Read more

Testing Spring’s @RequestBody using Spring MockMVC

Use this one public static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName(“utf8”)); @Test public void testInsertObject() throws Exception { String url = BASE_URL + “/object”; ObjectBean anObject = new ObjectBean(); anObject.setObjectId(“33”); anObject.setUserId(“4268321”); //… more ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false); ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter(); String requestJson=ow.writeValueAsString(anObject ); mockMvc.perform(post(url).contentType(APPLICATION_JSON_UTF8) .content(requestJson)) .andExpect(status().isOk()); } As described … Read more

How is Spring Cloud Gateway different from Zuul?

I am the author of spring cloud gateway. Zuul is built on servlet 2.5 (works with 3.x), using blocking APIs. It doesn’t support any long lived connections, like websockets. Gateway is built on Spring Framework 5, Project Reactor and Spring Boot 2 using non-blocking APIs. Websockets are supported and it’s a much better developer experience … Read more

Load different application.yml in SpringBoot Test

One option is to work with profiles. Create a file called application-test.yml, move all properties you need for those tests to that file and then add the @ActiveProfiles annotation to your test class: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration @IntegrationTest @ActiveProfiles(“test”) // Like this public class MyIntTest{ } Be aware, it will additionally load the application-test.yml, … Read more

How to use Session attributes in Spring-mvc

If you want to delete object after each response you don’t need session, If you want keep object during user session , There are some ways: directly add one attribute to session: @RequestMapping(method = RequestMethod.GET) public String testMestod(HttpServletRequest request){ ShoppingCart cart = (ShoppingCart)request.getSession().setAttribute(“cart”,value); return “testJsp”; } and you can get it from controller like this … Read more

Neither BindingResult nor plain target object for bean name available as request attribute [duplicate]

In the controller, you need to add the login object as an attribute of the model: model.addAttribute(“login”, new Login()); Like this: @RequestMapping(value = “https://stackoverflow.com/”, method = RequestMethod.GET) public String displayLogin(Model model) { model.addAttribute(“login”, new Login()); return “login”; }

Setting Precedence of Multiple @ControllerAdvice @ExceptionHandlers

Is this how one would expect Spring MVC to behave? As of Spring 4.3.7, here’s how Spring MVC behaves: it uses HandlerExceptionResolver instances to handle exceptions thrown by handler methods. By default, the web MVC configuration registers a single HandlerExceptionResolver bean, a HandlerExceptionResolverComposite, which delegates to a list of other HandlerExceptionResolvers. Those other resolvers are … Read more

Spring 5.0.3 RequestRejectedException: The request was rejected because the URL was not normalized

Spring Security Documentation mentions the reason for blocking // in the request. For example, it could contain path-traversal sequences (like /../) or multiple forward slashes (//) which could also cause pattern-matches to fail. Some containers normalize these out before performing the servlet mapping, but others don’t. To protect against issues like these, FilterChainProxy uses an … Read more

Which is better, return “ModelAndView” or “String” on spring3 controller

There is no better way. Both are perfectly valid. Which one you choose to use depends which one suits your application better – Spring allows you to do it either way. Historically, the two approaches come from different versions of Spring. The ModelAndView approach was the primary way of returning both model and view information … Read more

Spring MVC or Spring Boot [closed]

My personal advice is to definitely use Spring Boot for many reasons. The first is that Boot is the “future of Spring”. That means that with Boot you can benefit from many commitments of the Spring community. Most of the Spring projects today are completely integrated with Boot, even the community starts to develop many … Read more