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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:14:48+00:00 2026-05-19T03:14:48+00:00

I have a Spring MVC application which communicates with the frontend with AJAX /

  • 0

I have a Spring MVC application which communicates with the frontend with AJAX / JSON, and I have in the frontend a web application with HTML.

For adding an element to the database, I do this in the backend system:

@RequestMapping(value="add/", method=RequestMethod.POST)
public @ResponseBody SerializePerson addProject(@RequestBody Person person) {
    Person p = this.personService.addPerson(person);
    return new SerializePerson(p.getId(), p.getName(), p.getEmail());
}

But now I have the problem (this is a very simple example), that someone can create a project without a name, so the name = “” and a non valid email address.
My problem is, that I want to validate the fields in the backend system.

So I found a Spring MVC showcase here: https://src.springsource.org/svn/spring-samples/mvc-showcase/src/main/java/org/springframework/samples/mvc/validation/

They do this:

@RequestMapping("/validate")
public @ResponseBody String validate(@Valid JavaBean bean, BindingResult result) {
    if (result.hasErrors()) {
        return "Object has validation errors";
    } else {
        return "No errors";
    }
}

So, is this the best way? So I have to do two steps:

  1. validate the Person object (if no errors occur, go to step 2, otherwise show error message to user)
  2. write the Person object to the datbase

Isn’t it possible to combine these two steps in one step? And how can I put the Person POST object from the frontend to the “validate” method in the backend and to see which field fails (name or email), because telling only “Object has validation errors” is not so good :-)?

Best Regards.

  • 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-19T03:14:49+00:00Added an answer on May 19, 2026 at 3:14 am

    I did it shown in this example: http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/

    @RequestMapping(method=RequestMethod.POST)
    public @ResponseBody Map<String, ? extends Object> create(@RequestBody Account account, HttpServletResponse response) {
        Set<ConstraintViolation<Account>> failures = validator.validate(account);
        if (!failures.isEmpty()) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return validationMessages(failures);
        } else {
            accounts.put(account.assignId(), account);
            return Collections.singletonMap("id", account.getId());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Spring MVC to build my web application, and I have a
I have a Spring Web MVC application that I'd like to serve a large,
we have a J2EE web application usig Spring MVC. We have a new requirement
I am using Spring Web MVC for my application. I have 1 dropdown list
I basically have the following flow: XML -> JSON -> Spring MVC -> jsp
Hello I'm building a Spring MVC web application that runs on Tomcat 6.0.20 and
I'm building a MVC web application (using the Spring MVC framework), and I'm a
I am using Jboss and Netbeans to create Spring MVC web application on windows.
In a web application written using spring-MVC, I want to allow users to change
I have written a VERY simple MVC application which just displays a single string

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.