2

I have a web service using jar-rs. How do I throw a custom http error code to the calling application?

Thanks

1 Answer 1

5

The Response class lets you create a response with a specific HTTP Status. You can also extend WebApplicationException.

There are a couple of examples in the JAX-RS 1.0 features overview docs, in the 'Building Responses' and 'WebApplicationException and mapping Exceptions to Responses' sections that will tell you all you need to know to get started.

Sign up to request clarification or add additional context in comments.

3 Comments

Simplest way is probably to just throw a new WebApplicationException(int statusCode)
is there a way to set the HTTP Status code without "throwing an error"? I don't need the message showing up in the console.
Sure, when building your Response, you'll be using a Response.ResponseBuilder - use the status(Response.Status status) method to set the HTTP status code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.