请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

Put请求数据格式异常

 Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "{id}"]

json:

{
"id": 1,
"auditStatusEnum": "PASS",
"reason": "这是一个优秀的赚钱工具"
}
@RestController
@RequestMapping("/admin/shares")
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class ShareAminController {

    private final ShareService shareService;

    @PutMapping("/audit/{id}")
    public Share auditById(@PathVariable Integer id,@RequestBody ShareAuditDTO auditDTO){
        // TODO 认证、授权

        return this.shareService.auditById(id,auditDTO);
    }
}


Json传到后端的数据是String,但是id是Integer,这是json格式的问题吗

为什么视频里直接都不需要id了

我吧json去掉id,只传递auditStatusEnum和reason也还是报错

如果id改成String类型,调用auditById的时候进行转换是能用的,但是为啥Integer就用不了呢


@PathVariable如果替换成@RequestBody则是另一个错误

Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.lang.Integer` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.Integer` out of START_OBJECT token
 at [Source: (PushbackInputStream); line: 1, column: 1]]


id改成String类型又有其它的错误,这个错误应该是RocketMQ引起的,但是RocketMQ是正常启动的呀...

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.itmuch.contentcenter.dto.messaging.UserAddBounsMsgDTO$UserAddBounsMsgDTOBuilder and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)


正在回答 回答被采纳积分+3

2回答

大目 2020-09-11 13:37:18
{
"id": 1,
"auditStatusEnum": "PASS",
"reason": "这是一个优秀的赚钱工具"
}


为啥RequestBody里面有id这个field吗?

这是第一,从您截图的代码来看,你请求的url应该是/admin/shares/id 

消息体是

{
"auditStatusEnum": "PASS",
"reason": "这是一个优秀的赚钱工具"
}


0 回复 有任何疑惑可以回复我~
  • 提问者 _季步 #1
    没太明白,请求的url是admin/shares/audit/{id},请求路径应该是没错的,因为有请求到相应的控制器。您的消息体为什么不用传递id,而且url里面也没有id,那视频中的id是怎么获取到的呢!
    回复 有任何疑惑可以回复我~ 2020-09-12 13:22:39
  • 大目 回复 提问者 _季步 #2
    @Data
    public class ShareAuditDTO {
        /**
         * 审核状态
         */
        private AuditStatusEnum auditStatusEnum;
        /**
         * 原因
         */
        private String reason;
    }
    
    我不清楚你的ShareAuditDTO是怎么写的。我课上的代码是这样的。
    这里面没有id,id是放在@PathVariable上的,不在消息体里。
    建议学习下Spring MVC的基础用法。
    回复 有任何疑惑可以回复我~ 2020-09-12 23:45:59
提问者 _季步 2020-09-10 17:18:36
return this.shareService.auditById(Integer.valueOf(id),auditDTO);

虽然valueOf()可以解决,方式有更好解决方式吗?造成这样的原因是什么

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号