【问题标题】:Spring boot - Rest Service - Getting random HTTP status 400Spring boot - 休息服务 - 获取随机 HTTP 状态 400
【发布时间】:2018-09-27 22:08:39
【问题描述】:

我找不到原因...但我随机收到 Http 400,参数不存在:

Required String parameter 'id' is not present. 

我在 Postman 和不同的 Rest Client 中得到这个,

这是我的代码:

 @RequestMapping(path = "/borrower", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
   @CrossOrigin(origins = "*")
    public ResponseEntity<?> borrower(@RequestParam(value = "id") String cuit, @RequestHeader("Authorization") String token)  throws Exception {

           // some business code
       return ResponseEntity.ok().build();
    }

我使用的是 Spring Boot 2.0.1

【问题讨论】:

  • 如果将传入参数从cuit 重命名为id 会发生什么? (在borrower(...) 方法签名中)
  • 我可以试试。
  • 你确定你的代码可以编译吗?
  • @MarkoPacak 已修复
  • 第一步,一如既往,尽量把@ResponseBody放在返回类型ResponseBody&lt;?&gt;之前

标签: java spring-boot postman


【解决方案1】:

如果你这样尝试呢?

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<?> getStuff(@PathVariable("id") long id) { 
    //the long id defined is the variable passed when making the petition through postman
    Entity entity =  entityService.getById(id);
    //for the entity it is supposed to be your model, and service
    if (entity == null) {
        return new ResponseEntity<ErrorDTO>(new ErrorDTO(
         // the ErrorDTO is a file where you get all the descriptions
                "did not find any id  " + id), HttpStatus.NOT_FOUND);

    }
    return new ResponseEntity<Entity>(entity, HttpStatus.OK);

}

因为我有一些反对意见,所以我无法发表评论,而且人们不理解我所说的......所以换句话说,它是这样的......已编辑

【讨论】:

  • 欢迎@Cesar Kemper,请描述您的解决方案:)。
猜你喜欢
  • 2018-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-11
  • 1970-01-01
  • 2018-12-09
  • 1970-01-01
相关资源
最近更新 更多