JAVA SPRING REST Controller中的IllegalArgumentException

时间:2017-06-05 08:10:27

标签: java spring rest

我收到以下异常:

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:471) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:667) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_51]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_51]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_51]

我的REST控制器:

@GetMapping(path = "/users/{name}")
public @ResponseBody SystemUser getUserByName(@PathVariable("name") String name) {

    List<SystemUser> users = dataProvider.findSystemUsersByName(name);
    if (users != null && users.size() > 0) {            
        return users.get(0);
    }
    else {          
        return null;
    }
}

@GetMapping(path = "/usersByEmail/{email}")
public @ResponseBody SystemUser getUserByEmail(@PathVariable("email") String email) {

    List<SystemUser> users = dataProvider.findSystemUsersByEmail(email);
    if (users != null && users.size() > 0) {            
        return users.get(0);
    }
    else {          
        return null;
    }
}

我传递的简单字符串没有任何特殊字符(也没有&#39; @&#39;)作为参数。

有人能告诉我为什么我会得到上述异常以及如何修复它。非常感谢你!

1 个答案:

答案 0 :(得分:0)

我发现了问题!

实际上是在客户端实现中。

我错误地在我的angularjs客户端中编写了以下代码:

// Retrieves user by name
function GetByUsername(username) {
    return $http.get('http://localhost:5555/demo/users/{name}' + username).then(
        handleSuccess, handleError('Error getting user by username'));
}

// Retrieves user by email
function GetByEmail(email) {
    return $http.get('http://localhost:5555/demo/usersByEmail/{email}' + email).then(
        handleSuccess, handleError('Error getting user by email'));
}

而不是

// Retrieves user by name
function GetByUsername(username) {
    return $http.get('http://localhost:5555/demo/users/' + username).then(
        handleSuccess, handleError('Error getting user by username'));
}

// Retrieves user by email
function GetByEmail(email) {
    return $http.get('http://localhost:5555/demo/usersByEmail/' + email).then(
        handleSuccess, handleError('Error getting user by email'));
}