[英]Rest api call and POST request (java)
I am pretty new concerning REST api and POST request. 关于REST api和POST请求,我还很新。 I have the url of a REST api.
我有一个REST API的网址。 I need to access to this api by doing an API call in JAVA thanks to a client id and a client secret (I found a way to hash the client secret).
由于客户端ID和客户端密钥,我需要通过在JAVA中进行API调用来访问此api(我发现了一种对客户端密钥进行哈希处理的方法)。 However, as I am new I don't know how to do that api call.
但是,由于我是新手,所以我不知道该如何进行api调用。 I did my research during this all day on internet but I found no tutorial, website or anything else about how to do an api call.
我整天都在互联网上进行研究,但没有找到有关如何进行api调用的教程,网站或其他信息。 So please, does anyone know a tutorial or how to do that?
因此,请问有人知道一个教程或如何做吗? (if you also have something about POST request it would be great)
(如果您也有关于POST请求的信息,那就太好了)
I would be very thankful. 我将非常感谢。
Thank you very much for your kind attention. 非常感谢您的关注。
Sassir 萨西尔
Here's a basic example snippet using JDK classes only. 这是仅使用JDK类的基本示例代码段。 This might help you understand HTTP-based RESTful services a little better than using a client helper.
与使用客户端帮助程序相比,这可能会帮助您更好地了解基于HTTP的RESTful服务。 The order in which you call these methods is crucial.
调用这些方法的顺序至关重要。 If you have issues, add a comments with your issue and I will help you through it.
如果您有任何问题,请在问题中添加评论,我们将为您提供帮助。
URL target = new URL("http://www.google.com");
HttpURLConnectionconn = (HttpURLConnection) target.openConnection();
conn.setRequestMethod("GET");
// used for POST and PUT, usually
// conn.setDoOutput(true);
// OutputStream toWriteTo = conn.getOutputStream();
conn.connect();
int responseCode = conn.getResponseCode();
try
{
InputStream response = conn.getInputStream();
}
catch (IOException e)
{
InputStream error = conn.getErrorStream();
}
The Restlet framework also allows you to do such thing thanks to its class ClientResource
. Restlet框架还可以通过其类
ClientResource
来执行此类操作。 In the code below, you build and send a JSON content within the POST request: 在下面的代码中,您在POST请求中构建并发送JSON内容:
ClientResource cr = new ClientResource("http://...");
SONObject jo = new JSONObject();
jo.add("entryOne", "...");
jo.add("entryTow", "...");
cr.post(new JsonRepresentation(jo), MediaType.APPLICATION_JSON);
Restlet allows to send any kind of content (JSON, XML, YAML, ...) and can also manage the bean / representation conversion for you using its converter feature (creation of the representation based on a bean - this answer gives you more details: XML & JSON web api : automatic mapping from POJOs? ). Restlet允许发送任何类型的内容(JSON,XML,YAML等),还可以使用其转换器功能(基于Bean的表示形式的创建)为您管理Bean /表示形式的转换-此答案为您提供了更多详细信息: XML和JSON网络api:从POJO自动映射? )。
You can also note that HTTP provides an header Authorization
that allows to provide authentication hints for a request. 您还可以注意到HTTP提供了一个标头
Authorization
,它可以为请求提供身份验证提示。 Several technologies are supported here: basic, oauth, ... This link could help you at this level: https://templth.wordpress.com/2015/01/05/implementing-authentication-with-tokens-for-restful-applications/ . 此处支持几种技术:基本,oauth,...此链接可以在此级别为您提供帮助: https : //templth.wordpress.com/2015/01/05/implementing-authentication-with-tokens-for-restful-应用程序/ 。
Using authentication (basic authentication for example) can be done like this: 使用身份验证(例如基本身份验证)可以像这样完成:
String username = (...)
String password = (...)
cr.setChallengeResponse(ChallengeScheme.HTTP_BASIC, username, password);
(...)
cr.post(new JsonRepresentation(jo), MediaType.APPLICATION_JSON);
Hope it helps you, Thierry 希望对您有帮助,蒂埃里
I believe you want to make rest call from java application. 我相信您想从Java应用程序进行调用。 You can use any http client utility to implement.
您可以使用任何http客户端实用程序来实现。 For example: Apache Commons http client .
例如: Apache Commons http client 。
You can also use RestTemplate from Spring: https://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate 您也可以从Spring使用RestTemplate: https ://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate
Fast and simple solution without any boilerplate code. 快速简单的解决方案,无需任何样板代码。 Simple example:
简单的例子:
RestTemplate rest = new RestTemplate();
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("firstParamater", "parameterValue");
map.add("secondParameter", "differentValue");
rest.postForObject("http://your-rest-api-url", map, String.class);
If you intend to use Spring MVC to build REST Web Services then this article will be a good one. 如果您打算使用Spring MVC构建REST Web服务,那么本文将是不错的文章。
http://www.springbyexample.org/examples/contact-rest-services.html http://www.springbyexample.org/examples/contact-rest-services.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.