springmvc:RestFul-Filter

1.RestFul(简洁,安全,高效)

原来的:http://localhost:8888/test1?a=1&b=3
RestFull:http://localhost:8888/test1/2/2
@Controller
public class RestFullController {
   // @RequestMapping(value = "/test1/{a}/{b}",method = RequestMethod.GET)
    @RequestMapping("/test/{a}/{b}")
    public String test1(@PathVariable int a, @PathVariable int b, Model model){
        int res=a+b;
        model.addAttribute("msg","结果1为"+res);
        return "test";
    }
    @PostMapping("/test/{a}/{b}")
    public String test2(@PathVariable int a, @PathVariable int b, Model model){
        int res=a*b;
        model.addAttribute("msg","结果2为"+res);
        return "test";
    }
}

2.Filter(解决请求中的乱码)

<!--1.配置过滤器-->
<filter>
    <filter-name>encoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
    <param-name>encoding</param-name>
    <param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
//class配置
public class Filt implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
        filterChain.doFilter(request,response);
    }
    public void destroy() {

    }
}
原文地址:https://www.cnblogs.com/zh93/p/12952182.html