You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2020/01/11 03:07:38 UTC

[GitHub] [servicecomb-java-chassis] whuxiari opened a new issue #1522: 接口返回值定义的是ResponseEntity,为什么返回值的Body为空?

whuxiari opened a new issue #1522: 接口返回值定义的是ResponseEntity,为什么返回值的Body为空?
URL: https://github.com/apache/servicecomb-java-chassis/issues/1522
 
 
   返回代码参考:return ResponseEntity.status(status).body(message);

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [servicecomb-java-chassis] liubao68 commented on issue #1522: 接口返回值定义的是ResponseEntity,为什么返回值的Body为空?

Posted by GitBox <gi...@apache.org>.
liubao68 commented on issue #1522: 接口返回值定义的是ResponseEntity,为什么返回值的Body为空?
URL: https://github.com/apache/servicecomb-java-chassis/issues/1522#issuecomment-575428095
 
 
   下面有个例子可以参考下:
   
   ```
     @ResponseHeaders({@ResponseHeader(name = "h1", response = String.class),
         @ResponseHeader(name = "h2", response = String.class)})
     @RequestMapping(path = "/responseEntity", method = RequestMethod.POST)
     public ResponseEntity<Date> responseEntity(InvocationContext c1, @RequestAttribute("date") Date date) {
       HttpHeaders headers = new HttpHeaders();
       headers.add("h1", "h1v " + c1.getContext().get(Const.SRC_MICROSERVICE));
   
       InvocationContext c2 = ContextUtils.getInvocationContext();
       headers.add("h2", "h2v " + c2.getContext().get(Const.SRC_MICROSERVICE));
   
       return new ResponseEntity<>(date, headers, HttpStatus.ACCEPTED);
     }
   ```
   
   注意:
   1. 需要显示声明ResponseEntity的模板类型;
   2. 如果还需要返回额外的headers,需要通过ResponseHeaders显示的声明返回的header。 
   
   这个是java-chassis和Spring MVC不同的地方, 接口定义需要是明确的,能够通过契约描述出来实际的类型和header。 
   
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [servicecomb-java-chassis] whuxiari commented on issue #1522: 接口返回值定义的是ResponseEntity,为什么返回值的Body为空?

Posted by GitBox <gi...@apache.org>.
whuxiari commented on issue #1522: 接口返回值定义的是ResponseEntity,为什么返回值的Body为空?
URL: https://github.com/apache/servicecomb-java-chassis/issues/1522#issuecomment-575066267
 
 
   通过这样返回,在前端查看Body为空。不是null,是啥都没有。    
   public static ResponseEntity<String> getResponseEntity(XXXXException e) {
           return ResponseEntity.status(HttpStatus.valueOf(e.getCode())).body(e.getMsg());
       }
   
   改成这样抛出异常,前端是可以获取到Body的:
   throw new InvocationException(e.getCode(), e.getMessage(), e.getMsg());

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [servicecomb-java-chassis] liubao68 commented on issue #1522: 接口返回值定义的是ResponseEntity,为什么返回值的Body为空?

Posted by GitBox <gi...@apache.org>.
liubao68 commented on issue #1522: 接口返回值定义的是ResponseEntity,为什么返回值的Body为空?
URL: https://github.com/apache/servicecomb-java-chassis/issues/1522#issuecomment-573287185
 
 
   能将完整的接口原型定义和客户端调用方式的代码贴一下吗?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services