首页 > 代码库 > springmvc中@PathVariable传Double精度丢失

springmvc中@PathVariable传Double精度丢失

页面请求

http://localhost:8080/test/3.201

后端接受数据

/**
     * 测试
     *
     * @param number
     */
    @RequestMapping(value = "/test/{number}", method = RequestMethod.GET)
    public void test(@PathVariable Double number) {
        System.out.println("数字:" + number);
    }

结果

数字:3.0

改为下面的方式就可以

/**
     * 测试
     *
     * @param number
     */
    @RequestMapping(value = "/test/{number:.+}", method = RequestMethod.GET)
    //或者@RequestMapping(value = "http://www.mamicode.com/test/{number:.*}", method = RequestMethod.GET)
    public void test(@PathVariable Double number) {
        System.out.println("数字:" + number);
    }

建议传递带小数类型,最好用实体类接受,属性放在实体类中,用 BigDecimal 类型,数字更精确

 

springmvc中@PathVariable传Double精度丢失