2017-05-18 7 views
0

SpringのMVCコントローラに次のリクエストを送信しようとすると、次の例外が発生します。 JSONのペイロードには '\'文字が含まれていますが、これは過去にJerseyと一緒に働いていました。私はいくつかの一般的なSpringブート設定が欠けているのだろうかと思います。Spring MVC ControllerはJSON.Stringify()によって生成されたJSONを処理できません。

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); 
    } 
+0

このリクエストを試すことができますか? 'curl -X GET --header '' Content-Type:application/json '--header' Accept:application/json '-d' {" phone ":" + 4489325678 "} '' http:// localhost:8080/api/api/v1/auth/code'' – Zico

+1

なぜ '-d'を使って' GET'リクエストでデータを送信したいのですか? – rakesh

+0

Ajaxリクエスト(例えば、 'JSON.stringify()')を使用しているときに同じエラーが発生します。 – john

答えて

0

より多くのカール問題があるようです。 GETリクエストでデータを送信しようとしています。 -X GETを指定しない - -dフラグを使用してデータを提供している場合はcurlをデフォルトでPOSTに設定するか、-X POSTを明示的に使用します(-X PATCHもデータをサポートします)。

+0

Swagger UIを使用すると同じエラーが発生します。 CURLに固有のものではありません。 – john