2017-04-20 24 views
0
@POST 
    @Path("/{companyid}/location") 
    public Response addLocationCommand(LocationCommandDto addLocationCommand, @PathParam("companyid") String companyID) throws Throwable{ 
     addLocationCommand.setCompanyId(companyID); 
     Response response = processApiRequest(addLocationCommand, LocationCommandDtoToAddLocationCommandConverter.class, 
       AddLocationCommandProcessor.class); 
     return response; 

對象現在我在JSON格式參數轉換到REST

{ 
    "locationNam1e": "ytjtjtyj", 
    "locationType":"new", 
    "timeZone":"", 
    "mondayWorkingType":"halfday" 
} 

現在我LocationCommandDto類包含以下屬性發送請求。

public class LocationCommandDto implements CommandDTO { 

     private String companyId; 
     private String locationName; 
     private String locationType; 
     private String timeZone; 
} 
現在

在JSON我有屬性爲「locationNam1e」,這是不正確的,在我的LocationCommandDto是LOCATIONNAME,因此如何將它們映射並拋出異常,如果請求體的參數沒有類屬性的匹配,我們正在轉換??

回答

0

這樣的事情應該在你的ObjectMapper

ObjectMapper objectMapper = getObjectMapper(); 
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true); 
0

如果你使用Spring引導您可以按照您的application.properties文件屬性設置進行配置:

spring.jackson.deserialization.fail-on-unknown-properties=true