Spring MVC Controller无法处理JSON.Stringify()生成的JSON

时间:2017-05-18 05:59:26

标签: java spring spring-mvc spring-boot

当我尝试将以下请求发送到Spring Boot MVC控制器时,我一直收到以下异常。 JSON有效负载包含' \'人物,但这在过去与泽西岛一起工作。我想知道我错过了一些常见的Spring Boot配置。

  curl -X GET --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \ 
   "phone": "+4489325678" \ 
 }' 'http://localhost:8080/api/api/v1/auth/code'




 {
  "timestamp": 1495086719419,
  "status": 400,
  "error": "Bad Request",
  "exception": "org.springframework.http.converter.HttpMessageNotReadableException",
  "message": "Bad Request",
  "path": "/tappter/api/v1/auth/code"
}

我的控制器配置如下。

@Controller
@RequestMapping(value = "/auth", consumes = "application/json", produces = "application/json")
@Api(value = "/auth", description = "Authentication", tags = "Authentication", produces = "application/json", consumes = "application/json")
public class AuthResourceImpl implements IAuthResource {

    private final IAccessCodeService accessCodeService;
    private final IUserAccountService userAccountService;
    private final ITokenAuthenticationService tokenAuthenticationService;

    @Autowired
    public AuthResourceImpl(IAccessCodeService accessCodeService, IUserAccountService userAccountService,
                            ITokenAuthenticationService tokenAuthenticationService) {
        this.accessCodeService = accessCodeService;
        this.userAccountService = userAccountService;
        this.tokenAuthenticationService = tokenAuthenticationService;
    }

    @Override
    @ApiOperation("Request Access Code")
    @ApiResponses(value = {
        @ApiResponse(code = 201, message = "Access Code Sent", response = AccessCodeResponse.class),
        @ApiResponse(code = 400, message = "Invalid Number", response = Errors.class),
        @ApiResponse(code = 500, message = "Internal server error")
    })
    @RequestMapping(value = "/code", method = RequestMethod.GET)
    public @ResponseBody
    ResponseEntity<AccessCodeResponse> requestAccessCode(@Valid @RequestBody AccessCodeRequest request) {
        AccessCode newAccessCode = accessCodeService.create(request.getPhone());
        return new ResponseEntity<>(new AccessCodeResponse(newAccessCode.getAccessCode()), CREATED);
    }

1 个答案:

答案 0 :(得分:0)

似乎更像卷曲问题。您尝试在GET请求中发送数据。如果您使用-X GET标记提供数据,或者明确使用POST,则要么不提供-d - curl将默认为-X POST为了完整起见,-X PATCH也支持数据。