I am using a 3rd party grid, which generates the following GET API when a character is input :
http://localhost:4200/api/v1/driver/getDriverListForDropDown?firstName=[{"field":"DriverCode","operator":"startsWith","value":"12345","caseSensitive":false}]
Request param generated :
firstName:[{"field":"DriverCode","operator":"startsWith","value":"*","caseSensitive":false}]
I am trying to create a rest API on spring boot, but not able to hit the API.
I tried the following :
@GetMapping(path = "/getDriverListForDropDown")
public ResponseEntity<RestApiResponse<List<DriverDataForDropDown>>> getDriverListForDropDown(
@ApiIgnore Context context, @RequestParam(value="firstName") List<DriverDropdownFilterRequest> firstName)
DriverDropdownFilterRequest :
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
@JsonIgnoreProperties(ignoreUnknown = true)
@Builder(toBuilder = true)
public class DriverDropdownFilterRequest
{
String field;
String operator;
String value;
}
but I always get 400 error and on backend the following error :
java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986 at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:468) ~[tomcat-embed-core-9.0.29.jar:9.0.29] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:260) ~[tomcat-embed-core-9.0.29.jar:9.0.29] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.29.jar:9.0.29] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860) [tomcat-embed-core-9.0.29.jar:9.0.29] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1591) [tomcat-embed-core-9.0.29.jar:9.0.29] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.29.jar:9.0.29] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_171] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_171] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.29.jar:9.0.29]
I tried several options, but nothing is working. Please help.
CodePudding user response:
I presume, as your exception suggests, check whether your request format is valid (maybe the char '[' and ']' in firstName params contradicts), or your tomcat version support your format, please refer to this post.
Suggestion: Maybe instead of assigning hash-like value to params, you can separate them by creating new params (not only first name), or by setting the API point capable of accepting a body of json. e.g.
@GetMapping(path = "/get")
public ResponseEntity<...> get(..., @RequestBody Map<...> firstname)
{
.
.
.
}
cheers!
CodePudding user response:
Issue was the 3rd party was not properly encoding the parameter and tomcat was denying these characters.
Even when adding the relaxed characters in application.properties didn't worked.
the only solution that worked was to add the following config :
@Component
public class TomcatWebServerCustomizer implements
WebServerFactoryCustomizer<TomcatServletWebServerFactory>
{
/**
* Customize the specified
* @param factory the web server factory to customize
*/
@Override
public void customize(TomcatServletWebServerFactory factory)
{
factory.addConnectorCustomizers(connector ->
connector.setAttribute("relaxedQueryChars", "<>[\\]^`{|}"));
}
}
