首页 文章

当响应头内容类型设置为json时,Spring引导控制器返回xml

提问于
浏览
0

我有2个 Spring 季休息服务 . 一个是json内容的制作者,另一个是消费者 .

生产环境 者对控制器操作的相关代码如下:

@RequestMapping(value = "/cars", method = RequestMethod.POST)
public ResponseEntity<Cars> getCars(..methods params...){

 .....some code here.....

   HttpHeaders respHeader = new HttpHeaders();
   respHeader.set("Content-Type", "application/json");

   ResponseEntity<Cars> resp = new ResponseEntity<Cars>(cars, respHeader, HttpStatus.OK);
   return resp;

}

在消费者Spring启动服务代码中,我使用resTemplate来调用此 endpoints 并获取结果 . 如果你注意到上面的 生产环境 者代码,我在RequestMapping属性中没有produce =“application / json” . 但是,我在响应头中确实有Content-type作为“application / json” .

消费者获取xml响应而不是json . Jackson 在 class 道路上 .

我想知道什么解释返回xml而不是json,即使响应头中的Content-type设置为“application / json” .

如果此响应头没有任何效果,那么在RequestMapping xml而不是json中,yield属性的默认值是什么?

1 回答

  • -1
    @RequestMapping(value = "/cars", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Cars> getCars(..methods params...){
    
     .....some code here.....
    
       ResponseEntity<Cars> resp = new ResponseEntity<Cars>(cars, respHeader, HttpStatus.OK);
       return resp;
    
    }
    

相关问题