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 9092897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:57:08+00:00 2026-06-16T22:57:08+00:00

I implemented a REST application using Spring MVC, Jersey and JAXB. The client sends

  • 0

I implemented a REST application using Spring MVC, Jersey and JAXB.

The client sends a ServiceRequest object which contains information about the request and receives a ServiceResponse object back with information about the response.

ServiceRequest

@XmlRootElement(name = "servicerequest")
public class ServiceRequest{

    String serviceName = "AddUser"

    public String getServiceName() {
        return serviceName;
    }

    @XmlElement
    public void setServiceName(String serviceName) {
        this.serviceName = name;
    }   
}

ServiceResponse

@XmlRootElement(name = "serviceresponse")
public class ServiceResponse {

    String responseCode; 

    public String getResponseCode() {
        return responseCode;
    }

    @XmlElement
    public void setResponseCode(String code) {
        this.responseCode = name;
    }   
}

The client makes the call to the service using Jersey

ServiceClient

ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(RESOURCE_URL);

ServiceRequest request = new ServiceRequest();
ServiceResponse response = service.path("addUser").type(MediaType.APPLICATION_XML)
.accept(MediaType.APPLICATION_XML).entity(request).post(ServiceRequest.class);

On the server side, the service gets the request, handles it and returns a ServiceResponse object.

@RequestMapping(method = RequestMethod.POST, 
            value = "/addUser",headers="Accept=application/xml")
    @ResponseStatus(HttpStatus.OK)  
public @ResponseBody ServiceResponse addUser(@RequestBody ServiceRequest request) {

        ServiceResponse response = new ServiceResponse();

        //Handle request
        //service.addUser();

        response.setCode("200");
        return response; 
}

The controller class shown above makes a call to another service class to handle the request (service.addUser()). This service class can raise a number of exceptions.

What i am not sure of is how best to handle them. After googling around, i found that i can use an ExceptionHandler as shown below:

@ExceptionHandler(NullPointerException.class)
@ResponseBody
public String handleException1(NullPointerException ex)
{
    return ex.getMessage();
}

I can have the controllers extend a base class that has the exception handler.

A couple of questions regarding the above approach:

  • Do i have to create one handler for each exception type? There are so many exceptions that i need to handle so is it not possible to just have a generic handler for all exceptions?
  • As my controller returns an object of type ServiceResponse, what would be the object type that would be returned when an exception occurs?
  • The client expects to get an object of ServiceResponse type. How does it work if the response is an exception type?

And finally, i noticed that the ExceptionHanlder can only have a return type that is one of the following:

ModelAndView
Model
Map
View
String – interpreted as a view name
void, but only if the method writes directly to the response object

I thought i can create the ServiceResponse object in the ExceptionHandler and set the appropriate code and return the ServiceResponse object. This however is not possible if i cant return a ServiceResponse type in the excpetion handler.

Edit

I tried using the ExceptionHandler using a generic exception class as shown below

@ExceptionHandler(ServiceException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
    public @ResponseBody resolveServiceValidationException(ServiceException ex) {       

        System.out.println("In resolveServiceError");

        ServiceResponse = new ServiceResponse();
        response.setResponseException(ex.getMessage());
        return response;        
    } 

The exception is being caught but the ServiceResponse object is always null. All i get is the following message:

GET http://localhost:8080/myService/services/pingError returned a response status of 400 Bad Request

How can i access the response object when an exception is returned back?

Thanks

  • 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-16T22:57:09+00:00Added an answer on June 16, 2026 at 10:57 pm
    1. @ExceptionHandler annotation can take an array of classes as a parameter, so you can create only one (or more) handlers for multiple exceptions. Change the method signature to take the common ancestor of all handled exception classes as a parameter. Something along the lines:

      @ExceptionHandler(value = {NullPointerException.class, AnotherException.class})
      @ResponseBody
      public String handleException1(Exception ex) {
          return ex.getMessage();
      }
      
    2. In your case it’ll be String, since you annotated it with @ResponseBody, but you probably want to change the returned http code to some error code. You can do it by adding

      @ResponseStatus(value = HttpStatus.WHATEVER_CODE_YOU_WANT, reason = "Your message")` 
      

      annotation to your exception handler.

    3. In case when http code returned by the call is greater or equal than 300, jersey client throws UniformInterfaceException. You can catch and handle it. The response type doesn’t matter in this case, as the exception is thrown before the conversion.

    update

    Once you get a hold of UniformInterfaceException and change the exception handler on the server so it returns ServiceResponse as response body, then you can get the reponse using:

    ServiceResponse response = uniformInterfaceException.getResponse().getEntity(ServiceResponse.class);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Library application which is already implemented in spring MVC. I need
I wrote a rest service using jersey 1.13 and spring 3.1.1 which runs on
I have a JAX-RS REST service implemented using Jersey. One of the cool features
BACKGROUND: I have a REST API implemented in Java using Jersey. My API uses
I used Spring and Apache CXF to create a REST webservices application. I'm using
I'm writing web application that uses Spring MVC to bind Spring beans with REST-like
I'm trying to implement REST API using Jersey with Spring on Tomcat but I'm
I am using a GWT for client side application and REST web service instead
I've implemented a REST-based API (using Tonic, FWIW) so I have a central dispatch.php
I'm looking to implement a REST client in PHP, and have previously been using

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.