【问题标题】:field missing in the json response spring bootjson响应spring boot中缺少字段
【发布时间】:2021-01-07 15:43:56
【问题描述】:

我的 Stock 响应类只有两个字段,如下所示

class StockResponse {

private String orderId;
private String status;

//constructor

//getters and setters

}

还有控制器如下

@RestController
 @RequestMapping("/stocks")
 public class StockController {

 private static List<StockResponse> stocktList = new ArrayList <StockResponse > ();
 
 static {
     stocktList.add(new StockResponse("order1", "AVAILABLE"));
     stocktList.add(new StockResponse("order2", "AVAILABLE"));
     stocktList.add(new StockResponse("order3", "NOT AVAILABLE"));
     stocktList.add(new StockResponse("order4", "AVAILABLE"));
    
 }

 @GetMapping("/")
 public ResponseEntity < ? > getProsucts() {

  return ResponseEntity.ok(stocktList);

 }

 @GetMapping(path="/{id}", produces = "application/json;charset=UTF-8")
 public StockResponse getProsucts(@PathVariable String id) {

     StockResponse product = findOrder(id);
     
  if (product == null) {
 //  return ResponseEntity.badRequest(product)
 //   .body("Invalid product Id");
  }
  System.out.println(product.getOrderId());
  System.out.println(product.getStatus());
  

  return new StockResponse(product.getOrderId(), product.getStatus());

 }

 private StockResponse findOrder(String id) {
  return stocktList.stream()
   .filter(user -> user.getOrderId()
    .equals(id))
   .findFirst()
   .orElse(null);
 }


}

当我调用 localhost:8082/stocks/order1 时,我得到一个响应,其中只有一个字段显示如下

我会错过什么?

【问题讨论】:

    标签: json spring-boot postman rest get-mapping


    【解决方案1】:

    我无法重现这个,这意味着它对我有用。

    列出所有股票

    $ curl -sS 'http://localhost:8080/stocks/' | jq "."
    [
      {
        "orderId": "order1",
        "status": "AVAILABLE"
      },
      {
        "orderId": "order2",
        "status": "AVAILABLE"
      },
      {
        "orderId": "order3",
        "status": "NOT AVAILABLE"
      },
      {
        "orderId": "order4",
        "status": "AVAILABLE"
      }
    ]
    

    获取特定库存

    $ curl -sS 'http://localhost:8080/stocks/order1' | jq "."
    {
      "orderId": "order1",
      "status": "AVAILABLE"
    }
    

    我的StockController 和你的一模一样。我还复制并粘贴了您的 StockResponse 以获得与您相同的字段名称,但由于您没有包含构造函数/getter 和 setter,我将展示适合我的字段名称。

    您在stocktList 列表中实例化StockResponse 对象的方式是使用构造函数,这可能表明您实际上并未在对象上设置this.status。如果这不起作用,请仔细检查状态字段的 getter 是否实际称为 getStatus()

    public class StockResponse {
    
        private String orderId;
        private String status;
    
        public StockResponse(String orderId, String status) {
            this.orderId = orderId;
            this.status = status;
        }
    
        public String getOrderId() {
            return orderId;
        }
    
        public void setOrderId(String orderId) {
            this.orderId = orderId;
        }
    
        public String getStatus() {
            return status;
        }
    
        public void setStatus(String status) {
            this.status = status;
        }
    }
    

    您的回复包含一个在第一个字母中使用“非标准”大写的字段这一事实告诉我,您可能正在做其他不标准的事情,可能会影响您的结果。

    【讨论】:

    • 谢谢,我仔细看了看,我错过了public String getStatus() 的公众号
    猜你喜欢
    • 1970-01-01
    • 2017-05-26
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多