一个controller能同时使用两个@requestbody吗

时间:2024-10-14 11:46:14

看图:

controller层

    @RequestMapping("/testJson")
    public void testJson(@RequestBody Product product, String gender, @RequestBody ProductOrder order){
        System.out.println(order);
        System.out.println(gender);
        System.out.println(product);

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

控制台打印

 at [Source: (PushbackInputStream); line: 2, column: 1]]
2021-01-21 09:34:35.777  WARN 15168 --- [nio-8081-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `com.techman.tmy.entity.Product` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.techman.tmy.entity.Product` out of START_ARRAY token
 at [Source: (PushbackInputStream); line: 2, column: 1]]
  • 1
  • 2
  • 3

实体类Product

package com.techman.tmy.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.util.Date;
@Data
public class Product {
    private  int id;
    private String category;
    private String name;
    private  String productNo;
    private   String description;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8" )
    private Date createTime;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

实体类ProductOrder

package com.techman.tmy.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.util.Date;
@Data
public class ProductOrder {
    private int id;
    private int productId;
    private int editionId;
    private String orderNo;
    private String status;
    private String description;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8" )
    private Date createTime;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8" )
    private  Date modifyTime;}`  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18