java – 如何使用http post将多个参数传递给restful webservice
发布时间:2020-12-15 04:11:58 所属栏目:Java 来源:网络整理
导读:我有两个数组参数和对象数组,我想传递它们 我可以使用这样的方法吗? @POST @Path("Test3") @Produces("text/plain") @Consumes({"application/json"}) public String Test3(String[] id1,String[] id2,Object [] oo) { String result = "Hello "; .... ....
我有两个数组参数和对象数组,我想传递它们
我可以使用这样的方法吗? @POST @Path("Test3") @Produces("text/plain") @Consumes({"application/json"}) public String Test3(String[] id1,String[] id2,Object [] oo) { String result = "Hello "; .... .... return result; } 什么是我应该传递给这个方法的相应的json 我尝试了很多jsons,我总是得到这样的错误 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>GlassFish Server Open Source Edition 4.0 - Error report</title><style type="text/css"><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 500 - Internal Server Error</h1><hr/><p><b>type</b> Exception report</p><p><b>message</b>Internal Server Error</p><p><b>description</b>The server encountered an internal error that prevented it from fulfilling this request.</p><p><b>exception</b> <pre>javax.servlet.ServletException: Servlet.init() for servlet entities.service.ApplicationConfig threw exception</pre></p><p><b>root cause</b> <pre>org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization. [[FATAL] Method public java.lang.String entities.service.ItemFacadeREST.Test3(java.lang.String,java.lang.String) on resource class entities.service.ItemFacadeREST contains multiple parameters with no annotation. Unable to resolve the injection source.; source='ResourceMethod{httpMethod=POST,consumedTypes=[application/json],producedTypes=[text/plain],suspended=false,suspendTimeout=0,suspendTimeoutUnit=MILLISECONDS,invocable=Invocable{handler=MethodHandler{handlerClass=class entities.service.ItemFacadeREST,handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@cdf30b]},handlingMethod=public java.lang.String entities.service.ItemFacadeREST.Test3(java.lang.String,java.lang.String),parameters=[Parameter [type=class java.lang.String,source=null,defaultValue=null],Parameter [type=class java.lang.String,defaultValue=null]],responseType=class java.lang.String},nameBindings=[]}']</pre></p><p><b>note</b> <u>The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.0 logs.</u></p><hr/><h3>GlassFish Server Open Source Edition 4.0 </h3></body></html> 注意 : 解决方法
问题是在HTTP请求主体中发送的JSON被封送到第一个方法参数.您的其他参数未在请求中注明它们的来源,这就是您收到错误的原因:
ItemFacadeREST contains multiple parameters with no annotation. 您最好的选择是将参数组合到一个JSON对象中;就像是: { "id1":[ "string1","string2" ],"id2":[ "string3","string4" ],"oo":[ { "object1":"" },{ "object2":"" } ] } 然后将您的方法更改为: @POST @Path("Test3") @Produces("text/plain") @Consumes({"application/json"}) public String Test3(MyJsonObject json) { //Parse the JSON object } 当然,您需要定义一个表示MyJsonObject的类. 这篇SO帖子对配置RESTful服务有很好的解释:https://stackoverflow.com/a/8194612/2378728 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |