参数转换为REST中的Object

时间:2017-04-20 05:53:48

标签: java rest

    @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格式发送请求

{
    "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,所以如果请求body的参数与我们正在转换的class属性不匹配,如何映射它们并抛出异常?

2 个答案:

答案 0 :(得分:0)

应在ObjectMapper

中配置此类内容
ObjectMapper objectMapper = getObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);

答案 1 :(得分:0)

如果你正在使用Spring Boot,你可以在application.properties文件中设置以下属性:

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