2016-05-25 16 views
1

我准备从json字符串中使用json杰克逊对象映射器在春天模型。验证模型,当它从春季json字符串中创建

我的代码是

ObjectMapper mapper = new ObjectMapper(); 
    String empValue = mapper.writeValueAsString(employeeMap); 
    Employees employee = mapper.readValue(empValue, Employees.class); 

,因为如果任何不匹配的数据,将其通过例外,在准备model.How我能做到这一点准备的员工,我想验证的JSON对象之前?

+0

我不确定我是否完全理解用例。我认为employeeMap是雇用值。那么为什么它不应该是有效的,如果writeValueAsString没有抛出任何异常?你的意思是业务验证? –

+0

假设age是Employee类的一个属性,并且我给这个属性赋了一个字符串值。当从json准备对象时,它通过异常但是从这个异常我无法理解员工的哪个属性容易出错 –

+0

您能指定您正在使用的杰克逊和春季版本 –

回答

0

你可以得到一些有价值的信息出来的异常主要是出于使用的getPathJsonMappingException类和它的子类(在com.fasterxml.jackson.databind.exc包下)的getPathReference方法

下面是两个例子:

public class ObjectMapperTest { 
    private ObjectMapper objectMapper; 

    @Before 
    public void setup() { 
     objectMapper = new ObjectMapper(); 
    } 

    @Test 
    public void testWrongDataType() { 
     String personStr = "{\"age\":\"100Y\",\"firstName\":\"Jackson\",\"lastName\":\"Pollock\",\"gender\":\"M\"}"; 
     try { 
      objectMapper.readValue(personStr, Person.class); 
     } catch (JsonMappingException jme) { 
       System.out.println(jme.getMessage()); 
       if(jme instanceof InvalidFormatException) { 
        InvalidFormatException ife = (InvalidFormatException)jme; 
        System.out.println("Mapping failure on field:" + ife.getPathReference()); 
        System.out.println("Expected type: " + ife.getTargetType()); 
        System.out.println("provided value: " + ife.getValue()); 
       } 
     }catch (Exception e){ 
      System.out.println(e.getMessage()); 
     } 
    } 

    @Test 
    public void testUnrecognizedProperty() { 
     String personStr = "{\"gae\":\"100\",\"firstName\":\"Jackson\",\"lastName\":\"Pollock\",\"gender\":\"M\"}"; 
     try { 
      objectMapper.readValue(personStr, Person.class); 
     } catch (JsonMappingException jme) { 
      System.out.println(jme.getMessage()); 
      if(jme instanceof PropertyBindingException) { 
       PropertyBindingException pbe = (PropertyBindingException) jme; 
       System.out.println("Mapping failure on field:" + pbe.getPathReference()); 
       System.out.println("UnExpected field: " + pbe.getPropertyName()); 
      } 
     }catch (Exception e){ 
      System.out.println(e.getMessage()); 
     } 
    } 
} 

的输出将是

testUnrecognizedProperty: 
======================== 
Unrecognized field "gae" (class Person), not marked as ignorable (4 known properties: "lastName", "gender", "firstName", "age"]) 
at [Source: {"gae":"100","firstName":"Jackson","lastName":"Pollock","gender":"M"}; line: 1, column: 9] (through reference chain: Person["gae"]) 
Mapping failure on field:Person["gae"] 
UnExpected field: gae 
testWrongDataType: 
======================== 
Can not construct instance of java.lang.Integer from String value '100Y': not a valid Integer value 
at [Source: {"age":"100Y","firstName":"Jackson","lastName":"Pollock","gender":"M"}; line: 1, column: 2] (through reference chain: Person["age"]) 
Mapping failure on field:Person["age"] 
Expected type: class java.lang.Integer 
provided value: 100Y