0

I am working on a react application that has a java backend. The react frontend sends a URL request to a java web service controller, and within that webservice controller, the variable types are defined. Since a URL request is a string I’m having trouble dealing with non-string values. And since Javascript is not a strongly-typed language like Java I cannot definie variable types on the front end.

Web Request Example:

localhost:8080/filteredCSV.json?status=UNDER_ACTIVE_REVIEW,AUTHORIZED_BY_DWB,UNDER_CONSTRUCTION,CONSTRUCTION_COMPLETED&engineerEmail=null&issues=null&date=null

Web Service Controller:

@RequestMapping(value = "/filteredCSV.json")
@ResponseBody
public WebserviceResponse<?>filteredCSV(
    @RequestParam(value="status") ArrayList status,                             
    @RequestParam(value="engineerEmail") ArrayList engineerEmail,
    @RequestParam(value="issues", required=false) Boolean issues,
    @RequestParam(value="date", required=false) Local Date date){

 return service.filteredCSV(status, engineerEmail, issues, date);
} 

If the Boolean or Date values are null, then null is getting passed as a string which causes a TypeMismatch Error and the program stops. I do not appear to have a way to change the string to to a non-string null value once it hits the web service controller. I understand why this is occurring, but I am not sure if there is a way to work around it so that I can pass null as a value and not null as a string. Am I just screwed? Is there not a way to deal with this?

20yco
  • 876
  • 8
  • 28

2 Answers2

0

You're not sending null as a value, you're sending a string that contains the word 'null.

Send no value "" instead of the string value 'null'. This is what the @RequestParameter is expecting when it should be ignoring a parameter.

@RequestParameter docs

If you can't edit the javascript you still have some options ... what spring is really doing there is data binding values from the http request to instances of Java variables. You can override or bypass this as you need. (This sort of approach is the 'old' way of doing this and the very problem spring annotations hope to solve).

@RequestMapping(value = "/filteredCSV.json")
@ResponseBody
public WebserviceResponse<?>filteredCSV(HttpRequest request){
    String date = request.getParameter('date');
    // Convert date if not 'null' here, and so on
}
zmf
  • 9,095
  • 2
  • 26
  • 28
  • thank you for the response. I made the changes you recommended but it results in a nullPointerException error. Using the debugger I see the requests coming back, the it hits the if statement : `if(!issues.equals("null")){issue = Boolean.valueOf(issues) }` Any thoughts? – reactFullStackDeveloper Jan 08 '19 at 20:58
  • @rralbrittonYeah I gave you the wrong method there should be request.getParameter('date'); – zmf Jan 08 '19 at 21:05
0

I like the suggestion posted by @zmf, However If want find other work arounds. Please refer some of the approaches I think will work:

Solution 1: You can introduce a filter, and parse all the request parameters as string. When you encounter null as string do not send the param further, it will be considered as null at controller level.

Solution 2: Replace RequestParam type like Date, Boolean with String. You can then process accordingly.