Return exception from JAX-RS Rest Service as JSON

前端 未结 4 1767
一生所求
一生所求 2021-01-16 03:19

Is it in some way possible that an exception thrown from a rest service is returned as JSON? I have a JAX-RS Rest Service where I would like to achieve this. When I throw it

4条回答
  •  臣服心动
    2021-01-16 04:05

    catch your exceptions, then build a response object in a standardized format such as

    error: {
        code: 'XXX',
        status: HTTPStatus,
        message: 'my error message'
    }
    

    And send it as a response with an error status (from Response.Status, usually 4xx or 5xx)

提交回复
热议问题