如何使用http post将多个参数传递给restful webservice

编程入门 行业动态 更新时间:2024-10-28 09:19:35
本文介绍了如何使用http post将多个参数传递给restful webservice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个数组参数和对象数组,我想传递它们我可以使用这样的方法吗

I have two array parameters and array of Objects and I want to pass them can I use method like this

@POST @Path("Test3") @Produces("text/plain") @Consumes({"application/json"}) public String Test3(String[] id1,String[] id2 ,Object [] oo) { String result = "Hello "; .... .... return result; }

我应该传递给这个方法的相应json是什么

and what is the corresponding json that I should pass to this method

我尝试了很多json,但总是遇到这样的错误

I try alot of jsons and I always get errors like this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "www.w3/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="www.w3/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,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,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&#40;&#41; 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&#40;java.lang.String,java.lang.String&#41; on resource class entities.service.ItemFacadeREST contains multiple parameters with no annotation. Unable to resolve the injection source.&#59; source=&#39;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&#40;java.lang.String,java.lang.String&#41;, parameters=[Parameter [type=class java.lang.String, source=null, defaultValue=null], Parameter [type=class java.lang.String, source=null, defaultValue=null]], responseType=class java.lang.String}, nameBindings=[]}&#39;]</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>

注意:我想从 Android 调用 http post

Note : I want to call http post from Android

推荐答案

问题在于 HTTP 请求正文中发送的 JSON 被封送至第一个方法参数.您的其他参数没有注释它们在请求中的来源,这就是您收到错误的原因:

The problem is that the JSON being sent in the HTTP Request body is marshaled to the first method parameter. Your other parameters are not annotated as to where they are coming from in the request, which is why you are getting the error:

ItemFacadeREST contains multiple parameters with no annotation.

您最好的选择是将参数组合成一个 JSON 对象;类似:

Your best option would be to combine the parameters into a single JSON object; something like:

{ "id1":[ "string1", "string2" ], "id2":[ "string3", "string4" ], "oo":[ { "object1":"" }, { "object2":"" } ] }

然后将您的方法更改为:

And then change your method to:

@POST @Path("Test3") @Produces("text/plain") @Consumes({"application/json"}) public String Test3(MyJsonObject json) { //Parse the JSON object }

当然,您需要定义一个表示 MyJsonObject 的类.

You will, of course, need to define a class that represents the MyJsonObject.

这篇 SO 帖子对配置 RESTful 服务有很好的解释:stackoverflow/a/8194612/2378728

This SO post has an excellent explanation of configuring RESTful services: stackoverflow/a/8194612/2378728

更多推荐

如何使用http post将多个参数传递给restful webservice

本文发布于:2023-10-09 21:06:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476788.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   如何使用   参数   http   webservice

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!