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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:40:48+00:00 2026-05-25T14:40:48+00:00

When making controller for spring mvc application I have to specify a new method

  • 0

When making controller for spring mvc application I have to specify
a new method every time I want to pass a new class instances from spring forms:

@RequestMapping(method=RequestMethod.POST)
public String mainPost(@Valid MyClass myClass, 
        BindingResult result, Model model) {
    // do the same stuff each time here
}

So I need to write a method for every possible MyClass in my application
that can be passed to controller, but all these methods do absolutely the same stuff
(they check for errors and pass this object to service layer).

How can I merge all this methods in one?

I believe that there is some solution similar to the following (that doesn’t work):

@RequestMapping(method=RequestMethod.POST)
public <T> String mainPost(@Valid T myObject, 
        BindingResult result, Model model) {
    // check errors and pass the object myObject to a service layer
}
  • 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-25T14:40:49+00:00Added an answer on May 25, 2026 at 2:40 pm

    You cannot have generic controller methods, but you can create generic controller:

    public abstract class AbstractController<T> {
        @RequestMapping(method=RequestMethod.POST) 
        public String mainPost(@Valid T myObject,
              BindingResult result, Model model) { ... } 
    }
    
    @Controller 
    @RequestMapping("/myclass")
    public class MyClassController extends AbstractController<MyClass> { ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a jquery ajax call to an mvc controller. I want to
MVC 3. VB.NET I have the following controller action in my application to send
I have completed the MVC Music Store application, and now am making my own
I'm making a call to an MVC controller method. The return type is FileStreamResult.
In ASP.NET MVC 2, to secure controller action, i have created a class RequirePermission
I have to start a new Spring MVC project and I've been reading about
i have just converted an app i was making from a navigation controller app
I'm making a Web Service using Spring MVC. Controllers are called directly by the
I have a jqGrid on an ASP.Net MVC view. I want to use to
Every controller class in my project derive from a base controller class, aptly named

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.