ResponseBuilder 在与实体对象一起使用时不起作用
ResponseBuilder is not working when used with entity object
我正在尝试使用 responsebuilder 创建响应。
当我在实体中传递字符串时它工作正常但是当我传递一些错误时 class 它不起作用。
这是代码
1) 工作正常
Response.status(400).entity("test").build();
2) 不工作
Response.status(400).entity(new MyCustomExceptions(400,"My bad request")).build();
上面的代码出现错误
org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE: MessageBodyWriter not found for media type=text/plain
当我用上面的代码调用此服务时,我得到的是 500 错误,而不是第一种情况下的 400.But,我得到的是正确的 400 错误。
- 只是想了解如果我将对象传递给实体那么我需要
覆盖 class MyCustomExceptions?
中的一些方法
- 如何从 MyCustomExceptions 对象创建响应?
- 如果我将字符串传递给实体,它会起作用 fine.Why?
您传递的 HTTPHeader 中 accept
参数的值是多少?看起来它 text/plain
并且您正在尝试 return 一个对象。
- 在请求中将
accept
值作为 application/json
传递。或者
更改您的代码以响应添加类型 Json/XML
Response.status(400).type(MediaType.APPLICATION_JSON).entity(new MyCustomExceptions(400,"My bad request")).build();
将异常映射到 HTTP 错误响应
JAX-RS 允许您定义 Java 异常到 HTTP 错误响应的直接映射。
通过扩展 ,您可以创建特定于应用程序的异常,这些异常会构建一个带有状态代码和可选消息作为响应正文的 HTTP 响应。
考虑到这一点,您可以抛出 BadRequestException
而不是返回 Response
扩展 并将映射到状态代码为 400
:
的 HTTP 响应
throw new BadRequestException("My bad request");
有关 JAX-RS 中错误处理的更多详细信息,请参阅此 。
WebApplicationException
的子类
为方便起见, 目前扩展了以下异常(并且可以扩展它们以创建您自己的异常):
RedirectionException
: 3xx
重定向 错误的状态代码
ClientErrorException
: 4xx
Client 错误的状态代码
BadRequestException
: 400
错误请求
ForbiddenException
: 403
禁止
NotAcceptableException
: 406
不可接受
NotAllowedException
: 405
方法不允许
NotAuthorizedException
: 401
未授权
NotFoundException
: 404
未找到
NotSupportedException
: 415
不支持的媒体类型
ServerErrorException
: 5xx
Server 错误的状态代码
InternalServerErrorException
: 500
内部服务器错误
ServiceUnavailableException
: 503
服务不可用
我正在尝试使用 responsebuilder 创建响应。 当我在实体中传递字符串时它工作正常但是当我传递一些错误时 class 它不起作用。
这是代码
1) 工作正常
Response.status(400).entity("test").build();
2) 不工作
Response.status(400).entity(new MyCustomExceptions(400,"My bad request")).build();
上面的代码出现错误
org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE: MessageBodyWriter not found for media type=text/plain
当我用上面的代码调用此服务时,我得到的是 500 错误,而不是第一种情况下的 400.But,我得到的是正确的 400 错误。
- 只是想了解如果我将对象传递给实体那么我需要 覆盖 class MyCustomExceptions? 中的一些方法
- 如何从 MyCustomExceptions 对象创建响应?
- 如果我将字符串传递给实体,它会起作用 fine.Why?
您传递的 HTTPHeader 中 accept
参数的值是多少?看起来它 text/plain
并且您正在尝试 return 一个对象。
- 在请求中将
accept
值作为application/json
传递。或者 更改您的代码以响应添加类型
Json/XML
Response.status(400).type(MediaType.APPLICATION_JSON).entity(new MyCustomExceptions(400,"My bad request")).build();
将异常映射到 HTTP 错误响应
JAX-RS 允许您定义 Java 异常到 HTTP 错误响应的直接映射。
通过扩展
考虑到这一点,您可以抛出 BadRequestException
而不是返回 Response
扩展 400
:
throw new BadRequestException("My bad request");
有关 JAX-RS 中错误处理的更多详细信息,请参阅此
WebApplicationException
的子类
为方便起见,
RedirectionException
:3xx
重定向 错误的状态代码ClientErrorException
:4xx
Client 错误的状态代码BadRequestException
:400
错误请求ForbiddenException
:403
禁止NotAcceptableException
:406
不可接受NotAllowedException
:405
方法不允许NotAuthorizedException
:401
未授权NotFoundException
:404
未找到NotSupportedException
:415
不支持的媒体类型
ServerErrorException
:5xx
Server 错误的状态代码InternalServerErrorException
:500
内部服务器错误ServiceUnavailableException
:503
服务不可用