Embracing the Messiness in Search of Epic Solutions

Tag: Spring MVC

  • Spring MVC: Failed to convert value of type ‘java.lang.String’ to required type ‘java.time.LocalDateTime’

    PROBLEM Given the following controller … When executing … … the web service call returns 400 Bad Request with the following error in the console log:- SOLUTION One solution is to change the data type from java.time.LocalDateTime to java.lang.String before parsing it to java.time.LocalDateTime. However, it is a little more verbose than I like. A […]

  • Spring MVC: Handling Joda Data Types as JSON

    PROBLEM Let’s assume we have the following bean that contains Joda’s LocalDate and LocalDateTime objects:- This simple Spring MVC rest controller creates this bean and returns the JSON data back to the client:- By default, the generated JSON looks like this:- How do we nicely format these values and still retain the correct data types […]

  • Spring Security: Handling 403 Error Page

    If you are already using Spring, then you might want to use Spring Security to secure your web resources. To do that, we specify the URI to be secured with <security:intercept-url/> tag:- When users without role ROLE_TOPSECRET access /top-secrets/kfc-secret, they will see this default error page:- This proves that Spring Security is doing its job. […]

  • Spring MVC: Centralizing Common Configurations using @ControllerAdvice

    Handling Exceptions using HandlerExceptionResolver Once upon a time, Spring 2.0 introduced HandlerExceptionResolver to handle thrown exceptions. While this approach works, it is like throwing a big net to catch all types of fish. If we are only interested in catching piranhas and clownfish, this approach becomes a little tedious because we need to weed out […]

  • MockMvc : Circular view path [view]: would dispatch back to the current handler URL [/view] again

    PROBLEM Let’s assume we want to test this controller:- Here’s the test file:- When executing this test, we get the following error:- SOLUTION The reason this is happening is because the uri “/help” matches the returned view name “help” and we didn’t set a ViewResolver when constructing the standalone MockMvc. Since MockMvcBuilders.standaloneSetup(…) doesn’t load Spring […]

  • Backbone: model.destroy() Always Call “error” Callback Function

    PROBLEM Let’s assume we invoke person.destroy() to delete a person:- … and let’s assume when person.destroy() is invoked, it will call /person/{personId} DELETE. Here’s how the Spring MVC controller API might look like:- When we execute person.destroy(), the error callback function will always get called even though the HTTP status is a 200 OK. Why? […]