Giter Site home page Giter Site logo

接口方法参数List<User>,调用后,后端变成了List<hashMap>了,导致类型不对报错,不知道什么原因? about dubbo-js HOT 10 CLOSED

apache avatar apache commented on July 21, 2024
接口方法参数List,调用后,后端变成了List了,导致类型不对报错,不知道什么原因?

from dubbo-js.

Comments (10)

creasy2010 avatar creasy2010 commented on July 21, 2024 2

或者这种方式
java('java.util.List',[
new User(...).__fields2java()
])

我们提倡的最佳实践是单参数. 这样能避免很多问题..

from dubbo-js.

creasy2010 avatar creasy2010 commented on July 21, 2024

@15168326318
试下这种方式呢.
java('java.util.List',[
java('com.xxxx.User', {
name: 'bar' ,
id:33
.....
});
])

转换时 , 如果不是翻译师生成的对象(如:list ,) 会直接把参数返回. 所以new user 会处理错误..

from dubbo-js.

hufeng avatar hufeng commented on July 21, 2024

@15168326318 我来定位下。

from dubbo-js.

15168326318 avatar 15168326318 commented on July 21, 2024

@creasy2010 你们应该按alibaba的dubbo支持的参数来实现,他们是没限制的。 微服务dubbo不太可能按你们的限制来做的,对吧。

from dubbo-js.

creasy2010 avatar creasy2010 commented on July 21, 2024

@15168326318 是的,只是建议;
 传任意参数都是可以调通的,但 开发体验却没那么好,就像你这个例子,我们在开发过程中需要关注hessian.js能识别的对象结构 而不是本能的写一个javascript的对象;
目前我们还没想到好的方案打通这一点?

from dubbo-js.

lileilei avatar lileilei commented on July 21, 2024

@creasy2010

var combineArray = function (type, value) {
  var values = null;
  if (value) {
    values = [];
    for (var i = 0, len = value.length; i < len; i++) {
      values.push(combine(type, value[i]));
    }
  }
  return {
    $class: '[' + (type.name || type),
    $: values
  };
};
`

values.push(combine(type, value[i]));的时候判断一下是否有__fields2java方法有则调用不知道能否解决这个问题?

from dubbo-js.

creasy2010 avatar creasy2010 commented on July 21, 2024

这是 Js-to-java模块的逻辑?

from dubbo-js.

lileilei avatar lileilei commented on July 21, 2024

@creasy2010 是的。应该阔以哦

from dubbo-js.

creasy2010 avatar creasy2010 commented on July 21, 2024

你看的真仔细,, 哈哈,
确实是可以的, 而且有一个更好的位置 .放这段逻辑 .. 下面会详细说明

上次跟胡峰讨论过这件事, 我把讨论的内容和例子在这里阐明下...

转换基本类型数据示例

翻译师其实做了两件事,

  1. 编译时, 把java接口代码转换为typescript文件;
  2. 运行时, 把js对象转换为hession.js协议能识别的对象;

而此issue中一直就是围绕2讨论, 因为针对java的基本类型, 我们并没有生成转换代码,需要手工去写hession.js识别的结构;

我们可以在argumentMap 方法中写转换的逻辑 .

示例代码

 
export const DemoProviderWrapper = {
  sayHello: argumentMap,
  test: argumentMap,
  echo: argumentMap,
  getUserInfo: argumentMap,
};

你上面的例子伪代码如下:

export const DemoProviderWrapper = {
  �getUserList:(userList:Array<User>)=>{
      // 注: 转换的逻辑是自动生成的 
      // TODO null undefinded的情况还要排除
      return java('java.util.List',userList.map(user=>user.__fields2java()));
   },
};

但是 遇到java多态 就变得麻烦了... 这也这是为什么现在没有做这一块...

为什么没有做

估算下. 上面的解决方案能解决97%的场景, 但碰到java多态就会被"破功"了.. 尤其是参数类型不能通过次序+类型来判断的情况 ,
@lileilei

from dubbo-js.

CodeLittlePrince avatar CodeLittlePrince commented on July 21, 2024

建议把这个场景放在demo里,一个上午老泪纵横 T T

from dubbo-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.