设置 HttpClientErrorException 的 Http 状态码为 499。

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

Setting Httpstatus 499 for HttpClientErrorException

问题

我在想是否有一种方法可以为HttpClientErrorException设置Http状态码499。我之所以问这个问题,是因为我在Junit中尝试进行边界检查,而499恰好是边界的结束。

尽管如此,我已经了解到在Spring Framework中不支持这个状态码。

我尝试过以下方式来实现:

HttpClientErrorException var = new HttpClientErrorException(HttpStatus.valueof(499));

但是在尝试运行JUnit时会出现错误,请参考下面的信息:

java.lang.IllegalArgumentException: No matching constant for [499]
英文:

I was wondering if there is a way to set Httpstatus 499 for HttpClientErrorException. The reason I ask is because I'm trying to do boundary check in Junit and 499 is the end of the boundary.

Though, I have read that this status code is not supported in Spring Framework.

I have tried doing it like this.

HttpClientErrorException var = new HttpClientErrorException(HttpStatus.valueof(499));

But trying will cause an error during JUnit. Please refer below.

java.lang.IllegalArgumentException: No matching constant for [499]

答案1

得分: 1

是的,在Spring中有一种设置的方式。根据文档,你可以使用:

public static HttpStatus valueOf(int statusCode)

在HttpStatus枚举中。

状态码 499 不在枚举列表中的原因是它在IANA注册表中不存在。

这里有一个类似的问题链接

英文:

Yes, there is a way to set it in Spring. According to the docs,
you can use:

public static HttpStatus valueOf(int statusCode)

in HttpStatus enum.

The reason why status 499 is not in the list of enums is its absence in the IANA registry

There is a similar question here.

答案2

得分: 0

我已尝试使用UnknownHttpStatusCodeException替代HttpClientErrorException,因为根据其描述,这是在使用未知(或自定义)HTTP状态码时抛出的异常。

这解决了我的问题。

英文:

I have tried using UnknownHttpStatusCodeException instead of the HttpClientErrorException, since base on its description, this is the exception thrown when an unknown(or custom) HTTP status code is used.

This solved my issue.

huangapple
  • 本文由 发表于 2020年8月26日 17:44:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63594887.html
匿名

发表评论

匿名网友

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

确定