apache / shenyu

Apache ShenYu is a Java native API Gateway for service proxy, protocol conversion and API governance.
https://shenyu.apache.org/
Apache License 2.0
8.41k stars 2.92k forks source link

[BUG] <not support dubbo primitive type > #2487

Closed meiping05 closed 2 years ago

meiping05 commented 2 years ago

Is there an existing issue for this?

Current Behavior

Interface

Student findById(long id)

Invoke Failure

Expected Behavior

No response

Steps To Reproduce

No response

Environment

ShenYu version(s): 2.4.2-SNAPSHOT

Debug logs

No response

Anything else?

No response

yu199195 commented 2 years ago

need more info

meiping05 commented 2 years ago

2021-11-30 10:30:52 [New I/O client worker #1-1] ERROR org.apache.shenyu.plugin.alibaba.dubbo.AlibabaDubboPlugin - dubbo failed using async genericInvoker() metaData=MetaData{id='1461601432558649344', appName='dubbo-sample-webapp', contextPath='/dubbo-sample-webapp', path='/dubbo-sample-webapp/student/findById', rpcType='dubbo', serviceName='com.meip.gateway.inf.StudentInterface', methodName='findById', parameterTypes='long', rpcExt='{"loadbalance":"random","retries":2,"timeout":3000,"sent":false}', enabled=true} param={"id":"10"} java.lang.RuntimeException: java.lang.RuntimeException: Illegal constructor: long java.lang.RuntimeException: Illegal constructor: long at org.apache.dubbo.common.utils.PojoUtils.newInstance(PojoUtils.java:580) at org.apache.dubbo.common.utils.PojoUtils.realize0(PojoUtils.java:472) at org.apache.dubbo.common.utils.PojoUtils.realize(PojoUtils.java:212) at org.apache.dubbo.common.utils.PojoUtils.realize(PojoUtils.java:99) at org.apache.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:104) at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$FilterChainNode.invoke(FilterChainBuilder.java:82) at org.apache.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38) at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$FilterChainNode.invoke(FilterChainBuilder.java:82) at org.apache.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:41) at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$FilterChainNode.invoke(FilterChainBuilder.java:82) at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:145) at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:100) at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:175) at org.apache.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:51) at org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:57) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.dubbo.common.threadlocal.InternalRunnable.run(InternalRunnable.java:41) at java.lang.Thread.run(Thread.java:748)

meiping05 commented 2 years ago

Interface

`public interface StudentInterface {

Student findById(long id);

List<Student> findByArrays(Long[] ids);

}`

impl

`@DubboService public class StudentInterfaceImpl implements StudentInterface {

@Override
@ShenyuDubboClient(path = "/student/findById")
public Student findById(long id) {
    return Student.builder().id(id).age(20).height(170).name("测试").sex(Sex.MALE).build();
}

@Override
@ShenyuDubboClient(path = "/student/findByArrays")
public List<Student> findByArrays(Long[] ids) {
    return Arrays.stream(ids).map(x -> Student.builder().id(x).age(20).height(170).name("测试").sex(Sex.MALE).build()).collect(Collectors.toList());
}

}`

yu199195 commented 2 years ago

this have error?

List findByArrays(Long[] ids);

zhaiweij commented 2 years ago

hi ,The error log contains' Illegal constructor: long ' , Your pojo object constructor has no parameters?

meiping05 commented 2 years ago

primitive type : long

public Student findById(long id) { return Student.builder().id(id).age(20).height(170).name("测试").sex(Sex.MALE).build(); }

meiping05 commented 2 years ago

If is Long Then true

public Student findById(Long id)

zhaiweij commented 2 years ago

Can see the Student ?

meiping05 commented 2 years ago

Request is error not Response

` @Data @Builder public class Student implements Serializable {

private long id;

private String name;

private int age;

private Sex sex;

private int height;

}

`

meiping05 commented 2 years ago

是否是基本类型拼装类型的问题

GenericService 这个接口只有一个方法,名为 $invoke,它接受三个参数,分别为方法名、方法参数类型数组和参数值数组;

对于方法参数类型数组

  1. 如果是基本类型,如 int 或 long,可以使用 int.class.getName()获取其类型;
  2. 如果是基本类型数组,如 int[],则可以使用 int[].class.getName();
  3. 如果是 POJO,则直接使用全类名,如 com.alibaba.dubbo.samples.generic.api.Params。
zhaiweij commented 2 years ago

You can add @ NoArgsConstructor to Student

meiping05 commented 2 years ago

Or it could be a Dubbo BUG

meiping05 commented 2 years ago

ApacheDubboProxyService

Line : 84

` return Mono.fromFuture(invokeAsync(genericService, metaData.getMethodName(), pair.getLeft(), pair.getRight()).thenApply(ret -> { if (Objects.isNull(ret)) { ret = Constants.DUBBO_RPC_RESULT_EMPTY; } exchange.getAttributes().put(Constants.RPC_RESULT, ret); exchange.getAttributes().put(Constants.CLIENT_RESPONSE_RESULT_TYPE, ResultEnum.SUCCESS.getName()); return ret; })).onErrorMap(exception -> exception instanceof GenericException ? new ShenyuException(((GenericException) exception).getExceptionMessage()) : new ShenyuException(exception));

`

pair.getRight() is wrong

BodyParamUtils.buildSingleParameter

yu199195 commented 2 years ago

can you fix it in BodyParamUtils.buildSingleParameter? @meiping05

meiping05 commented 2 years ago

I try it

meiping05 commented 2 years ago

Have Other Contact Information ?