0

I am not able to catch the exception when I send the below request. empno datatype is long. But am sending more than max of long value. Even I tried @Min and @Max annotation in my pojo class but it's not working. As per my requirement I should not use string instead of long.

{
    "empno": 135458464765456464654564654566
}

public class Personal
{
    private long empNo;
    public void setEmpNo(long empNo)
    {
        this.empNo = empNo;
    }
    
    public long getEmpNo()
    {
        return this.empNo;
    }
}
    
@RestController
@RequestMapping(value = "/employee/v1")
public class EmployeeController {
    
    public ResponseEntity<LocationResponse> getLocationService(HttpServletRequest httpServletRequest,
                @Valid @RequestBody Personal personal, HttpServletResponse httpResponse)
    {
     .... Business Logic
    }

}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

You should use BigInteger instead for larger numbers. Long vs BigInteger Alternatively, a workaround would be to read it in as a String in your request object and then parse it to a long, using Long.parseLong(stringValue) in your service tier, you can wrap that in a try/catch block then.

Gary O' Donoghue
  • 362
  • 2
  • 4
  • 15
  • I want to throw Bad request Exception with message instead of changing the datatype – Prem Kumar Feb 09 '18 at 08:25
  • You can still throw your bad request exception in the catch of that block – Gary O' Donoghue Feb 09 '18 at 09:00
  • am not able to catch the exception because its failing at the time of mapping (@RequestBody Personal personal) and it will not enter into inside controller method – Prem Kumar Feb 09 '18 at 10:57
  • No what I mean is change it to a String, then try parse that to a long, at which point it will fail and you can catch your exception and return a 400 bad request – Gary O' Donoghue Feb 09 '18 at 12:12
0

If you are not going to use String then you can use BigInteger to store values that exceed the max value of long. For Example :

public class Personal
{
    private BigInteger empNo;
    public void setEmpNo(BigInteger empNo)
    {
        this.empNo = empNo;
    }

    public BigInteger getEmpNo()
    {
        return this.empNo;
    }
}
Sample Exmaple :
BigInteger bd = new BigInteger("922337203685477582012312321");
System.out.println(bd.multiply(new BigInteger("15")));
System.out.println(bd);

Output

13835058055282163730184684815
922337203685477582012312321

Refer this link for more : https://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigInteger.html

Ramesh Fadatare
  • 561
  • 4
  • 12
  • I want to throw Bad request Exception with message instead of changing the datatype – Prem Kumar Feb 09 '18 at 10:57
  • Provide exception handling to "empno": 135458464765456464654564654566 this variable before sending json as response. – Ramesh Fadatare Feb 09 '18 at 11:51
  • am not able to catch the exception because its failing at the time of mapping (@RequestBody Personal personal) and it will not enter into inside controller method – Prem Kumar Feb 09 '18 at 12:23