Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8662449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:44:23+00:00 2026-06-12T16:44:23+00:00

I am playing around and converting an Spring MVC REST project to use Jersey,

  • 0

I am playing around and converting an Spring MVC REST project to use Jersey, and have a method in one of my classes annotated with

@ExceptionHandler(HttpMessageNotReadableException.class)

which will catch any exceptions during de-serialization of an XML message. This way I can detect when a user sends bad XML in a POST request and return an XML Error response as opposed to a 400/500 standard HTML error page.

Is there something similar in Jersey? I tried creating an exception mapping class

@Provider
public class HttpMessageNotReadableException 
      implements ExceptionMapper<org.xml.sax.SAXNotRecognizedException> {

but that doesn’t seem to catch any in-coming errors, just when I throw the exception from inside the resource class.

So I thought of having my method get the string and do the de-serialization and catch any errors:

@POST
@Path("/product")
public Response createProduct(String createRequest) {
      try {
         // de-serialize
         // do work ...
      } catch ...

instead of

@POST
@Path("/product")
public Response createProduct(CreateProduct createRequest) {
    // do work ...

but that doesn’t seem right, just going to pollute all my classes with the same error checking.

I also read about implementing my own reader using

@Provider
class MyXmlReader implements MessageBodyReader {

but that just seems like re-inventing the wheel since JAXB is already doing the de-serialization, I just want to catch the error.

Anyone know if a better way to catch bad incoming XML?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:44:24+00:00Added an answer on June 12, 2026 at 4:44 pm

    Jersey uses the exact same @Provider approach than Spring (which is defined in JAX-RS). I don’t know how Spring un/marshall objects, but Jersey uses JAXB for XML and JSON by default (it can use Jackson for JSON if you tell it to do so).

    Have you tried to catch JAXB exceptions?

    import javax.ws.rs.core.Response;
    import javax.ws.rs.ext.ExceptionMapper;
    import javax.ws.rs.ext.Provider;
    import javax.xml.bind.JAXBException;
    
    @Provider
    public class JAXBExceptionMapper implements ExceptionMapper<JAXBException> {
        public Response toResponse(JAXBException exception) {
            return Response.status(Status.BAD_REQUEST).build();
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just picked up Resharper and have been playing around converting fields to properties.
Playing around with Python - tkInter - Entry widget - when I use validatecommand
I have been playing around with zxing over the weekend, and have ran into
While playing around with one-to-one associations in castle activerecord I stumbled upon the following
Just playing around with Rails 3.1 at the moment, and one issue I'm having
I have been playing around with various methods of making the Visitor pattern in
So I was playing around with some code and wanted to see which method
Playing around with Google Maps these days, with some directions. I have a map
Just playing around with interfaces and I have a question about something which I
Whilst playing around in an open source project, my attempt to ToString a DateTime

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.