Java SpringMVC控制器无法处理JSON.Stringify()生成的JSON
当我尝试向我的Spring Boot MVC控制器发送以下请求时,我不断收到以下异常。JSON负载包含“\”字符,但这在过去对Jersey有效。我想知道我是否缺少一些常见的Spring引导配置Java SpringMVC控制器无法处理JSON.Stringify()生成的JSON,java,spring,spring-mvc,spring-boot,Java,Spring,Spring Mvc,Spring Boot,当我尝试向我的Spring Boot MVC控制器发送以下请求时,我不断收到以下异常。JSON负载包含“\”字符,但这在过去对Jersey有效。我想知道我是否缺少一些常见的Spring引导配置 curl -X GET --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \ "phone": "+4489325678" \ }' 'http://localhost:8
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);
}
@控制器
@RequestMapping(value=“/auth”,consumes=“application/json”,products=“application/json”)
@Api(value=“/auth”、description=“Authentication”、tags=“Authentication”、products=“application/json”、consumes=“application/json”)
公共类AuthResourceImpl实现IAuthResource{
专用最终IAccessCodeService accessCodeService;
私人最终IUserAccountService用户帐户服务;
私有最终ITokenAuthenticationService令牌AuthenticationService;
@自动连线
公共AuthResourceImpl(IAccessCodeService accessCodeService、IUserAccountService userAccountService、,
ITokenAuthenticationService令牌身份验证服务){
this.accessCodeService=accessCodeService;
this.userAccountService=userAccountService;
this.tokenAuthenticationService=tokenAuthenticationService;
}
@凌驾
@API操作(“请求访问代码”)
@ApiResponses(值={
@ApiResponse(code=201,message=“已发送访问代码”,response=AccessCodeResponse.class),
@ApiResponse(代码=400,消息=“无效数字”,响应=Errors.class),
@ApiResponse(代码=500,消息=“内部服务器错误”)
})
@RequestMapping(value=“/code”,method=RequestMethod.GET)
公共@ResponseBody
ResponseEntity requestAccessCode(@Valid@RequestBody AccessCodeRequest请求){
AccessCode newAccessCode=accessCodeService.create(request.getPhone());
返回新的ResponseEntity(新建AccessCodeResponse(newAccessCode.getAccessCode()),已创建);
}
似乎更像是一个curl问题。您试图在GET
请求中发送数据。要么不提供-X GET
-curl,如果您使用-d
标志提供数据,要么显式使用-X POST
(为了完整起见,-X补丁也支持数据)。你能试试这个请求吗?curl-X GET--header'Content-Type:application/json'-header'Accept:application/json'-d'{“phone”:“+4489325678”}' 'http://localhost:8080/api/api/v1/auth/code“
为什么要在GET
请求中使用-d
发送数据?使用Ajax请求时会发生相同的错误,例如JSON.stringify()
每次尝试Ajax GET请求时都无法传递数据。如果使用招摇过市UI,我会遇到相同的错误。这不是CURL特有的错误。