3

@RequestBody @Valid アノテーションを使用して、Spring で Bean の検証とデータ マッピングを行おうとしています。

エンティティ Bean でわかるように、@NotNull アノテーションと @Min アノテーションを使用しています。どちらも異なる検証メッセージを使用しており、コントローラーのBindingResult引数でこのメッセージを読み取ることができます。

しかし、クライアントが "operatorId" フィールドにlongではなくString値を送信すると、以下に示すエラーが表示されます。

私の質問は:

クライアントが operatorId に数値以外の文字列値を設定したときに、エラー メッセージ テキストをクライアントに返したいと考えています。(例:「数値などを送信する必要があります」)

エンティティ Bean :

@Entity(name="ua")
@Table(name="ua")
public class UserAgreement extends BaseContractEntity {

    @Column(name = "OPERATOR_ID")
    @NotNull(message = "operatorId must be provided")
    @Min(value = 1, message = "operatorId must be bigger then zero")
    protected long operatorId;


    //... getters setters, etc
}

コントローラー:

@RequestMapping(value = "/ug", method = RequestMethod.POST)
@ResponseBody
public ServiceResponse saveUserAgreement(@RequestBody @Valid UserAgreement userAgreement,
                                     BindingResult result) {

    ServiceResponse serviceResponse = new ServiceResponse();

    if(result.hasErrors()) {

        serviceResponse.setMessage(result.getAllErrors().get(0).getDefaultMessage());
        serviceResponse.setErrorCode("400");
        return serviceResponse;

    } else {

        // call service
        serviceResponse.setMessage("ok");
        return serviceResponse;
    }
}

これは私のリクエストの例です:

この json テキストを使用して POST リクエストを行う場合 (間違った operatorId 非数値! ):

{"content":"text content text","announcement":"YES","approved":"YES","operatorId":"5fg0"}

このエラーを取得:

    org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not construct instance of long from String value '5fg0': not a valid Long value
 at [Source: org.apache.catalina.connector.CoyoteInputStream@9ba3afe; line: 1, column: 69] (through reference chain: com.ggg.agr.core.model.UserAgreement["operatorId"]); nested exception is org.codehaus.jackson.map.JsonMappingException: Can not construct instance of long from String value '5fg0': not a valid Long value
 at [Source: org.apache.catalina.connector.CoyoteInputStream@9ba3afe; line: 1, column: 69] (through reference chain: com.ggg.agr.core.model.UserAgreement["operatorId"])
    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readJavaType(MappingJacksonHttpMessageConverter.java:169)
    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.read(MappingJacksonHttpMessageConverter.java:161)
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:135)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:180)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:95)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:123)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:680)
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not construct instance of long from String value '5fg0': not a valid Long value
 at [Source: org.apache.catalina.connector.CoyoteInputStream@9ba3afe; line: 1, column: 69] (through reference chain: com.ggg.agr.core.model.UserAgreement["operatorId"])
    at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
    at org.codehaus.jackson.map.deser.StdDeserializationContext.weirdStringException(StdDeserializationContext.java:243)
    at org.codehaus.jackson.map.deser.std.StdDeserializer._parseLong(StdDeserializer.java:351)
    at org.codehaus.jackson.map.deser.std.StdDeserializer$LongDeserializer.deserialize(StdDeserializer.java:846)
    at org.codehaus.jackson.map.deser.std.StdDeserializer$LongDeserializer.deserialize(StdDeserializer.java:833)
    at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299)
    at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1923)
    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readJavaType(MappingJacksonHttpMessageConverter.java:166)
    ... 38 more
4

1 に答える 1