简体   繁体   English

弹簧控制器@RequestParam中的类型不匹配

[英]Type mismatch in spring controllers @RequestParam

I am working on a spring webservice. 我正在从事Spring Web服务。 I have created a exception resolver for handling any exceptions so that exception are sent to the client in proper format. 我创建了一个用于处理所有异常的异常解析器,以便以正确的格式将异常发送到客户端。

The exception are being caught in service layer and customized message are being sent. 服务层中捕获了异常,并且正在发送自定义消息。
Problem is if the request params in controller are of numeric type and if a user sends a string, then the defaulf message for the exception is being sent. 问题是,如果控制器中的请求参数为数字类型,并且如果用户发送了字符串,则将发送异常的defaulf消息。 But i want to send a customized error. 但我想发送自定义错误。 Can anyone please suggest how should i do it. 任何人都可以建议我该怎么做。

I think you need something like this 我想你需要这样的东西

    @RequestMapping(value = "/happy}", method = RequestMethod.POST)
public String happy(@RequestParam(value = "somevalue") int number) {
    // implement your flow
    return null;
}

@RequestMapping(value = "/happy}", method = RequestMethod.POST)
public String unhappy(@RequestParam(value = "somevalue") String string) {
    // send a custom error message to the user to use a number instead of a String
    return null;
}

You could try using a Spring Handler Interceptor , extracting the parameter as a string from the request itself, and check if it's numeric. 您可以尝试使用Spring Handler Interceptor ,从请求本身中将参数提取为字符串,然后检查其是否为数字。 You could either have one interceptor for each case and watch only on those endpoints (simpler but more classes) or generalize it to validate all the endpoint's parameters over the request. 您可以为每种情况设置一个拦截器,然后仅在那些端点上进行监视(简单但更多的类),或者对其进行泛化以验证请求中所有端点的参数。

One per case example: 每个案例一个:

public class IntTypeCheckInterceptor extends HandlerInterceptorAdapter {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String myIntString = request.getParameter("myInt");
        Pattern intPattern = Pattern.compile("[0-9]+");
        Matcher intMatcher = intPattern.matcher(myIntString);

        //I *think* this does the whole input
        if(!intMatcher.matches()) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
            return false; //false tells spring to stop processing and returns to the client
        }

        return true; //true tells spring to continue as normal
    }

}

Config: 配置:

@Override
public void addInterceptors(InterceptorRegistry registry) {
    //with this only do where the request parameter is the same name and expected to be an int
    registry.addInterceptor(new IntTypeCheckInterceptor()).paths("/your-intpath", "your-intpath2");
}

The other approach would involve checking the handler is a HandlerMethod, pulling the parameter names and types, and checking each one. 另一种方法涉及检查处理程序是HandlerMethod,拉出参数名称和类型,并检查每个参数。

A filter should also work. 过滤器也应该起作用。 I'm just more familiar with interceptors and it's inside Spring so you have all the same features available from the app context. 我对拦截器更加熟悉,它位于Spring内部,因此您可以从应用程序上下文中获得所有相同的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM