1

I have following code to get double with 5 digit decimal but not getting

@RequestMapping(value = "/double1/{double}", method = RequestMethod.GET)
    @ResponseBody
    public double getDouble1(@PathVariable("double")double dou){
        System.out.println(dou);
        return dou;
    }

Following is input output

http://localhost:8080/svc/double1/4569999999999991.922222222333 ==> 4569999999999991
http://localhost:8080/svc/double1/4569999999999991.1234567 ==> 4569999999999991 

1 Answers1

1

Michael Laffargue's linked question (Spring MVC @PathVariable getting truncated) has the answer - the '.' in the double is being automatically parsed as the file extension (and then being interpreted by Spring MVC as an Accept header). You can disable this suffix pattern matching in either XML Spring config:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="useDefaultSuffixPattern" value="false" />
</bean>

or using JavaConfig in your WebMvcConfigurerAdapter:

@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
    configurer.setUseSuffixPatternMatch(false);
}

I'd also recommend using a BigDecimal instead of a double as suggested by Jon Skeet and Michael, due to the precision loss.

Community
  • 1
  • 1