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

  • Home
  • SEARCH
  • 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 7661239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:34:19+00:00 2026-05-31T13:34:19+00:00

I am working on a REST api using Spring-MVC and json. I running my

  • 0

I am working on a REST api using Spring-MVC and json. I running my automatic tests using Jetty and an in-memory database. I want to test that posting an invalid domain object gives me the error message from the @NotEmpty annotation. But all I get is the default Jetty 400 Bad Request page.

I have a domain class with some validation:

@Entity
public class Company {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotEmpty(message = "Name is a required field")
    private String name;

    /* getters, setters */
}

Here’s the controller

@Controller
public class CompanyController {
    @RequestMapping(value = "/company", 
        method = RequestMethod.POST, 
        consumes = "application/json")
    public void createCompany(
        @Valid @RequestBody final Company company,
        final HttpServletResponse response) {

        //persist company
        response.setStatus(HttpServletResponse.SC_CREATED);
        response.setHeader("location", "/company/" + company.getId());
    }
}

How can I get the value "Name is a required field" returned as part of the response?

  • 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-05-31T13:34:19+00:00Added an answer on May 31, 2026 at 1:34 pm

    This does it:

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public void exceptionHandler(final MethodArgumentNotValidException ex, 
                         final HttpServletResponse response) throws IOException {
    
        for (final ObjectError objectError : ex.getBindingResult().getAllErrors()) {
            response.getWriter().append(objectError.getDefaultMessage()).append(".");
        }
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have basic authentatication working with REST API using curl: curl -X POST -H
I'm working with the Azure REST API and they are using this to create
I have a custom auth provider working against a REST API and now want
I'm working on a REST API. The key objects ("nouns") are "items", and each
I'm working on building the URLs for my REST API before I begin writing
I'm getting started on building a REST API for a project I'm working on,
I am using WCF and REST, and I have complex types, which are working
I'm working with an external REST API that returns some data as a comma-separated
I'm working with Redmine's xml rest api. The service returns xml like in the
I'm working on a largely navigation-based iPhone app that communicates with a REST API

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.