NutzCN Logo
问答 接口的入参可以是List<对象>吗?
发布于 2846天前 作者 qq_37643c1b 2235 次浏览 复制 上一个帖子 下一个帖子
标签:

兽总,我在写一个接口,是给其他人用的,我想问问我的接口的参数是 list<对象> ,可以这样写不?

15 回复

能,然后走前缀表单或json

public Object addDo(@Param("..")List<Tab_order> tab_order) { 
		
		if(tab_order != null){
		
			dao.fastInsert(tab_order);// 那我就直接使用 tab_order 对象了 我就不需要一个个解析了吧,只是请求我的人要走json 形式,那我这样写的接口就有问题了。。
			
			return Result.success("system.success");
		}
		
		return Result.error("system.error");
	}

@qq_37643c1b 前缀表单

@wendal 我这里List tab_order 这个对象可能有多条数据 ,所以我才用list <对象>的,然后这么写个接口启动就保错了!

2017-03-09 09:25:21,383 org.nutz.mvc.impl.NutActionChainMaker.eval(NutActionChainMaker.java:51) DEBUG - Eval FAIL!! : public java.lang.Object cn.wizzer.modules.controllers.platform.api.TabOrderAPIController.addDo(java.util.List)
org.nutz.lang.born.BorningException: Fail to born 'java.util.List'
 by args: [] becasue: No suitable Constructor or Factory Method!!
	at org.nutz.lang.Mirror.getBorning(Mirror.java:954)
	at org.nutz.mvc.adaptor.injector.ObjectPairInjector.<init>(ObjectPairInjector.java:38)
	at org.nutz.mvc.adaptor.PairAdaptor.evalInjectorBy(PairAdaptor.java:54)
	at org.nutz.mvc.adaptor.AbstractAdaptor.evalInjector(AbstractAdaptor.java:192)
	at org.nutz.mvc.adaptor.AbstractAdaptor.init(AbstractAdaptor.java:131)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.evalHttpAdaptor(AdaptorProcessor.java:40)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.init(AdaptorProcessor.java:23)
	at org.nutz.mvc.impl.NutActionChainMaker.eval(NutActionChainMaker.java:38)
	at org.nutz.mvc.impl.UrlMappingImpl.add(UrlMappingImpl.java:54)
	at org.nutz.mvc.impl.NutLoading.evalUrlMapping(NutLoading.java:204)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:108)
	at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19)
	at org.nutz.mvc.NutFilter._init(NutFilter.java:87)
	at org.nutz.mvc.NutFilter.init(NutFilter.java:65)
	at org.eclipse.jetty.servlet.FilterHolder.initialize(FilterHolder.java:138)
	at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:852)
	at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:298)
	at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1349)
	at org.eclipse.jetty.maven.plugin.JettyWebAppContext.startWebapp(JettyWebAppContext.java:297)
	at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1342)
	at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:741)
	at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:505)
	at org.eclipse.jetty.maven.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:366)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
	at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
	at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:163)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
	at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
	at org.eclipse.jetty.server.Server.start(Server.java:387)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
	at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
	at org.eclipse.jetty.server.Server.doStart(Server.java:354)
	at org.eclipse.jetty.maven.plugin.JettyServer.doStart(JettyServer.java:73)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:535)
	at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:358)
	at org.eclipse.jetty.maven.plugin.JettyRunMojo.execute(JettyRunMojo.java:167)
	at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
	at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
	at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
	at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
	at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
	at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
	at org.apache.maven.cli.MavenCli.execute(MavenCli.java:862)
	at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286)
	at org.apache.maven.cli.MavenCli.main(MavenCli.java:197)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
	at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
	at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
2017-03-09 09:25:21,412 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:125) ERROR - Error happend during start serivce!
@Param("::user") List<User> list

表单

user[0].name=abc
user[0].age=100

@wendal 如果 不走 表单形式呢 如果是 json 我该怎么改呢?
因为我的List里面会又很多数据 要做一个快速保存的接口~

@qq_37643c1b json的话加适配器

@wendal 加了还是获取不到数据呢 对象还是null
代码如下:

@At("/platform/xxx/api")
	@AdaptBy(type = JsonAdaptor.class) // json 适配器
	public Object addDo(List<Tab_order> tab_order) {
		
		if(tab_order != null){
		
			
			dao.fastInsert(tab_order);
			
			return Result.success("system.success");
		}
		
		return Result.error("system.error");
	}

请求的json

{
    "tab_order": [
        {
            "pnr_no": 1,
            "airline": 123,
            "price": 1222,
            "tax_cost": 20
        },
        {
            "pnr_no": 2,
            "airline": 222,
            "price": 333,
            "tax_cost": 33
        }
    ]
}

默认是从根开始匹配的, 所以需要改成

@Param("tab_order") List<Tab_order> tab_order

@wendal 加了这个 @Param("tab_order") 对象还是null 不知道为什么了

有一种可能性, content-type没设置对

Content-Type的设置为 application/x-www-form-urlencoded

@qq_37643c1b 设置为application/json 这样才不会被其他代码意外吃掉

@wendal 已经泣不成声 , 原谅我这只小白吧 ~

@wendal 小小打赏 以表感谢

添加回复
请先登陆
回到顶部