Showing posts with label Spring MVC. Show all posts
Showing posts with label Spring MVC. Show all posts

Friday, November 10, 2017

Spring framework - Jackson UnrecognizedPropertyException not thrown

Jackson UnrecognizedPropertyException not thrown

This post is to show how to force newer version of Spring (4.1.1.RELEASE and later) to return error when a JSON with unrecognized fields is deserialized.

Setup

Maven
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>4.3.12.RELEASE</version>
</dependency>
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>4.3.12.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.2</version>
</dependency>

Development

User.java

package com.example.model;

public class User {
    private String id;
    private String name;

    // constructor, setters, getters
}

JSON

{
  “id”: “28b60d1e-c650-11e7-abc4-cec278b6b50a”,
  “name”: “John Smith”,
  “nickname”: “Johnny”
}

Controller.java

package com.example.controller;

@RestController
public class Controller {

    private UserService userService;

    @PutMapping(“/user”)
    public void updateUserInfo(@RequestBody User user) {
        userService.updateUser(user);
    }
}
I expected to see UnrecognizedPropertyException or 400 Bad Request when the JSON was sent in, but it did not happen. Adding the annotation @JsonIgnoreProperties(ignoreUnknown=false) to the POJO had no effect, either. It turns out Spring Framework has changed its default configuration. So in the Spring configuration xml, I have to override its behavior

context-config.xml

<mvc:annotation-driven">
 <mvc:message-converters>
  <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
              <property name="objectMapper">
               <bean class="com.fasterxml.jackson.databind.ObjectMapper"/>
              </property>
  </bean>
 </mvc:message-converters>
</mvc:annotation-driven>

Sunday, April 27, 2014

Resolve Element 'mvc:annotation-driven' must have no character

To resolve the error

cvc-complex-type.2.1: Element 'mvc:annotation-driven' must have no character or element information item [children], because the type's content type is empty.

chagne the mvc schemaLocation to newer version, for example, 3.2 works for me.
<beans xmlns="http://www.springframework.org/schema/beans"
        ...
 xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        ...
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

Wednesday, September 25, 2013

Spring MVC and Cookie

In a common scenario, a cookie may be set on the client side using JavaScript

function setCookie() {
    var c_name = "javascriptCookie";
    var c_value = "this cookie was set by JavaScript";
    document.cookie=c_name + "=" + c_value;
}

Spring MVC can also write cookie while returning a page

@RequestMapping(value="/setcookie", method = RequestMethod.GET)
public String setCookie(HttpServletResponse response){
    response.addCookie(new Cookie("srpingCookie", "this cookie was set by Spring"));
    return "cookieIsSet";
}

To read cookies on Spring

// all the cookies can be seen here regardless of that the cookie was set on client side or server side
@RequestMapping(value="/readcookie", method = RequestMethod.GET)
public String showCookie(HttpServletRequest request) {
    Cookie[] cookies = request.getCookies();
    if (cookies != null){
 for (Cookie cookie : cookies){
     System.out.println(cookie.getName() + " : " + cookie.getValue());
        }
    }
        
    ...
}

or

// mycookie must be available otherwise HTTP 400 occurs
// the cookie value will be unescape'd automatically
@RequestMapping(value="/readparticularcookie", method = RequestMethod.GET)
public String showDinoCookie(@CookieValue("mycookie") String cookie){
    System.out.println(cookie);

    ...
}

Reference
http://www.w3schools.com/js/js_cookies.asp