IllegalArgumentException:POST访问Spring Controller时,HTTP标头行不符合RFC 7230

时间:2019-12-19 12:27:32

标签: spring

我想用Spring实现用户注册功能。当我尝试将数据发布到spring控制器时,抛出了上述异常。令人惊讶的是,GET请求在控制器上工作。

@RestController
@RequestMapping(RestApi.ANONYMOUS + "register")
public class RegisterController {

@PostMapping(value="/new", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public UserRegistrationResultDto registerUser(@RequestBody UserRegisterDto dto) {
    UserRegistrationResultDto result = registerService.registerUser(dto.getMail(), 
                                       dto.getPassword(), dto.getRepeatedPassword());
    return result;
 }

@GetMapping("/test")
public String test() {
    return "get success";
 }
}

失败,错误代码为400的请求如下:

POST http://localhost:8080/api/anonymous/register/new
     Content-Type:application/x-www-form-urlencoded
{
mail=data&password=data&passwordRepeated=data
}

它引发以下异常:

java.lang.IllegalArgumentException: The HTTP header line [{: ] does not conform to RFC 7230 and has been ignored.

此获取请求有效:

GET http://localhost:8080/api/anonymous/register/test

我正在使用Tomcat 9作为服务器。

1 个答案:

答案 0 :(得分:0)

不确定如何提交POST请求...但是HTTP标头字段在:之后需要一个空格:

Content-Type: application/x-www-form-urlencoded