java restful服务中如何使用json参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28983048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to consume json parameter in java restful service
提问by Vivek Singh
How can i consume json parameter in my webservice, I can able to get the parameters using @PathParam but to get the json data as parameter have no clue what to do.
我如何在我的网络服务中使用 json 参数,我可以使用 @PathParam 获取参数,但是将 json 数据作为参数获取不知道该怎么做。
@GET
@Path("/GetHrMsg/json_data")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces(MediaType.APPLICATION_JSON)
public String gethrmessage(@PathParam("emp_id") String empid) {
}
}
What to use in place of @PathParam and how to parse it later.
用什么来代替 @PathParam 以及以后如何解析它。
采纳答案by bertag
I assume that you are talking about consuming a JSON message body sent with the request.
我假设您正在谈论使用随请求发送的 JSON 消息正文。
If so, please note that while not forbidden outright, there is a general consensus that GET requests should nothave request bodies. See the "HTTP GET with request body" question for explanations why.
如果是的话,请注意,虽然不是完全禁止的,有一个普遍的共识是GET请求应该不会有请求主体。有关原因的解释,请参阅“带有请求正文的 HTTP GET”问题。
I mention this only because your example shows a GET request. If you are doing a POST or PUT, keep on reading, but if you are really doing a GET request in your project, I recommend that you instead follow kondu's solution.
我提到这一点只是因为您的示例显示了 GET 请求。如果您正在执行 POST 或 PUT,请继续阅读,但如果您真的在项目中执行 GET 请求,我建议您改为遵循kondu 的解决方案。
With that said, to consume a JSON or XML message body, include an (unannotated) method parameter that is itself a JAXB bean representing the message.
话虽如此,要使用 JSON 或 XML 消息正文,请包含一个(未注释的)方法参数,该参数本身就是表示消息的 JAXB bean。
So, if your message body looks like this:
因此,如果您的消息正文如下所示:
{"hello":"world","foo":"bar","count":123}
Then you will create a corresponding class that looks like this:
然后您将创建一个如下所示的相应类:
@XmlRootElement
public class RequestBody {
@XmlElement String hello;
@XmlElement String foo;
@XmlElement Integer count;
}
And your service method would look like this:
您的服务方法如下所示:
@POST
@Path("/GetHrMsg/json_data")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public void gethrmessage(RequestBody requestBody) {
System.out.println(requestBody.hello);
System.out.println(requestBody.foo);
System.out.println(requestBody.count);
}
Which would output:
这将输出:
world
bar
123
For more information about using the different kinds of HTTP data using JAXB, I'd recommend you check out the question "How to access parameters in a RESTful POST method", which has some fantastic info.
有关使用 JAXB 使用不同类型 HTTP 数据的更多信息,我建议您查看问题“如何访问 RESTful POST 方法中的参数”,其中包含一些很棒的信息。
回答by kondu
@PathParam
is used to match a part of the URL as a parameter. For example in an url of the form http:/example.com/books/{bookid}
, you can use @PathParam("bookid")
to get the id of a book to a method.
@PathParam
用于匹配 URL 的一部分作为参数。例如,在表单的 url 中http:/example.com/books/{bookid}
,您可以使用@PathParam("bookid")
将书籍的 id 获取到方法。
@QueryParam
is used to access key/value pairs in the query string of the URL (the part after the ?). For example in the url http:/example.com?bookid=1
, you can use @QueryParam("bookid")
to get the value of `bookid.
@QueryParam
用于访问 URL 的查询字符串中的键/值对(? 之后的部分)。例如在 url 中http:/example.com?bookid=1
,您可以使用@QueryParam("bookid")
来获取`bookid 的值。
Both these are used when the request url contains some info regarding the parameters and you can use the data directly in your methods.
当请求 url 包含一些有关参数的信息并且您可以直接在方法中使用数据时,将使用这两种方法。
Please specify the problem in detail if this post doesn't help you.
如果这篇文章对您没有帮助,请详细说明问题。
回答by Young Emil
Bertag is right about the comment on the GET. But if you want to do POST request that consumes json data, then you can refer to the code below:
Bertag 对 GET 的评论是正确的。但是如果你想做消耗json数据的POST请求,那么你可以参考下面的代码:
@POST
@Path("/GetHrMsg/json_data")
@Consumes(MediaType.APPLICATION_JSON)
public Response gethrmessage(InputStream incomingData) {
StringBuilder crunchifyBuilder = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));
String line = null;
while ((line = in.readLine()) != null) {
crunchifyBuilder.append(line);
}
} catch (Exception e) {
System.out.println("Error Parsing: - ");
}
System.out.println("Data Received: " + crunchifyBuilder.toString());
// return HTTP response 200 in case of success
return Response.status(200).entity(crunchifyBuilder.toString()).build();
}
For referencing please click here
如需参考,请点击这里