AbstractHttpMessage getParams().setParameter construct depcrcated ~ How to modernize?

huangapple go评论38阅读模式
英文:

AbstractHttpMessage getParams().setParameter construct depcrcated ~ How to modernize?

问题

I am working to modernize old legacy code, and while doing that ran into some deprecated constructs that I can't figure out how to handle. They all look somewhat like this:

HttpPost oauthVerificationRequest = new HttpPost(authURL);
oauthVerificationRequest.getParams().setParameter(OAUTH_TOKEN_KEY, oauthToken);
HttpResponse oauthVerificationRequestResponse = client.getHttpClient().execute(oauthVerificationRequest);

There, my IDE complains that both getParams() as well as setParameter is deprecated.

The thing is, written as it is like this, I understand exactly what is happening. The deprecated line sets the value of the parameter with the key OAUTH_TOKEN_KEY of the request to the value of oauthToken, and probably creates it if it doesn't exist.

However, even knowing that this is what is supposed to happen in this line, I have been unable to find a way to write this line in a modern way. I've tried to figure it out, but the new way of constructing an AbstractHttpMessage simply confuses me.

Since I learn best by examples, could someone please provide me with a "translation" of the above code to the new logic?

英文:

I am working to modernize old legacy code, and while doing that ran into some deprecated constructs that I can't figure out how to handle. They all look somewhat like this:

		HttpPost oauthVerificationRequest = new HttpPost(authURL);
		oauthVerificationRequest.getParams().setParameter(OAUTH_TOKEN_KEY, oauthToken);
		HttpResponse oauthVerificationRequestResponse = client.getHttpClient().execute(oauthVerificationRequest);

There, my IDE complains that both getParams() as well as setParameter is deprecated.

The thing is, written as it is like this, I understand exactly what is happening. The deprecated line sets the value of the parameter with the key OAUTH_TOKEN_KEY of the request to the value of oauthToken, and probably creates it if it doesn't exist.

However, even knowing that this is what is supposed to happen in this line, I have been unable to find a way to write this line in a modern way. I've tried to figure it out, but the new way of constructing a AbstractHttpMessage simply confuses me.

Since I learn best by examples, could someone please provide me with a "translation" of the above code to the new logic?

答案1

得分: 0

Sure, here's the translation of the provided text:

好的,再次看来,将我的想法写成问题的形式似乎有助于将我的思路引导到正确的轨道上,以找到解决方案。所以回答我自己的问题,以新逻辑编写上述语句的正确方式应该是:

URIBuilder oauthVerificationRequestBuilder = new URIBuilder(authUrl);
oauthVerificationRequestBuilder.setParameter(OAUTH_TOKEN_KEY, oauthToken);
HttpPost oauthVerificationRequest = new HttpPost(oauthVerificationRequestBuilder.build());
HttpResponse oauthVerificationRequestResponse = client.getHttpClient().execute(oauthVerificationRequest);

基本上,首先创建一个构建器,然后在构建器内设置参数,最后使用 builder.build() 作为请求的参数来创建请求。

奖励问题:

是否还有一种方法将 addHeader() 修改添加到构建器中?因为目前,对我来说,整个结构看起来是这样的,对我来说,使用构建器设置参数,然后以“老式”的方式在请求上添加标题似乎有点不一致:

URIBuilder oauthVerificationRequestBuilder = new URIBuilder(authUrl);
oauthVerificationRequestBuilder.setParameter(OAUTH_TOKEN_KEY, oauthToken);
oauthVerificationRequestBuilder.setParameter(OAUTH_VERIFIER_KEY, oauthVerifier);
oauthVerificationRequestBuilder.setParameter(AUTHORIZE_KEY, VALUE_STRING_TRUE);
HttpPost oauthVerificationRequest = new HttpPost(oauthVerificationRequestBuilder.build());
oauthVerificationRequest.addHeader(CONTENT_TYPE_KEY, CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED_UTF_8);

请注意,上述代码中的注释可能已被我忽略,因为您要求只返回翻译的部分。

英文:

Okay, once again it seems that writing down my thoughts as a question helped get my mind down on the right track to find the solution. So to answer my own question, the correct way to write above statement in the new logic would be:

URIBuilder oauthVerificationRequestBuilder = new URIBuilder(authUrl);
oauthVerificationRequestBuilder.setParameter(OAUTH_TOKEN_KEY, oauthToken);
HttpPost oauthVerificationRequest = new HttpPost(oauthVerificationRequestBuilder.build());
HttpResponse oauthVerificationRequestResponse = client.getHttpClient().execute(oauthVerificationRequest);

So basically, you first create a builder, then set the parameters inside the builder, and then create the request using builder.build() as its parameter.

Bonus Question:

Is there also a way to get the addHeader() modification into the builder? Because right now, the entire construct looks like this for me, and it feels kinda inconsistent to use the builder for the parameters, and then slapping the header on top of the request the "old fashioned" way:

		URIBuilder oauthVerificationRequestBuilder = new URIBuilder(authUrl);
		oauthVerificationRequestBuilder.setParameter(OAUTH_TOKEN_KEY, oauthToken);
		oauthVerificationRequestBuilder.setParameter(OAUTH_VERIFIER_KEY, oauthVerifier);
		oauthVerificationRequestBuilder.setParameter(AUTHORIZE_KEY, VALUE_STRING_TRUE);
		HttpPost oauthVerificationRequest = new HttpPost(oauthVerificationRequestBuilder.build());
		oauthVerificationRequest.addHeader(CONTENT_TYPE_KEY, CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED_UTF_8);

答案2

得分: 0

Googling "java httppost" gives a link to the documentation。在文档中查找getParams(),显示它是从AbstractHttpMessage继承的,另一个Google搜索找到了该类的文档。该文档解释了如何代替使用已弃用的方法:

已弃用。 (4.3) 使用HttpClient提供的配置API的构造函数参数

我希望这对未来的读者有所帮助。优秀的库会记录已弃用方法的建议替代方案。查阅这些建议的文档始终是一个好主意。

英文:

Googling "java httppost" gives a link to the documentation. Looking for getParams() in the docs, shows that it is inherited from AbstractHttpMessage and another google search found the docs for that class. That docs explains what to do instead of using the deprecated method:

> Deprecated. (4.3) use constructor parameters of configuration API provided by HttpClient

I hope this helps some future reader. Good libraries will document what the suggested replacement is for deprecated methods. It's always a good idea to consult the documentation for these suggestions.

答案3

得分: 0

以下是您要翻译的内容:

"It's very simple by http-request built on apache http API.

HttpRequest httpRequest = HttpRequestBuilder.create(
ClientBuilder.create().build();
)
.build();

Response response = httpRequest.target(authURL)
.addParameter(OAUTH_TOKEN_KEY, oauthToken)
.addParameter(ANOTHER_PARAM, "VALUE")
.addHeader(ANY_HEADER, "VALUE")
.post();

Dependency

<dependency>
<groupId>com.jsunsoft.http</groupId>
<artifactId>http-request</artifactId>
<version>2.2.2</version>
</dependency>"

英文:

It's very simple by http-request built on apache http API.

HttpRequest httpRequest = HttpRequestBuilder.create(
  ClientBuilder.create().build();
)
.build();

Response response = httpRequest.target(authURL)
.addParameter(OAUTH_TOKEN_KEY, oauthToken)
.addParameter(ANOTHER_PARAM, &quot;VALUE&quot;)
.addHeader(ANY_HEADER, &quot;VALUE&quot;)
.post();

Dependency

&lt;dependency&gt;
  &lt;groupId&gt;com.jsunsoft.http&lt;/groupId&gt;
  &lt;artifactId&gt;http-request&lt;/artifactId&gt;
  &lt;version&gt;2.2.2&lt;/version&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年7月31日 13:40:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63186352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定