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

In ASP.NET MVC 2, to secure controller action, i have created a class RequirePermission
I have completed the MVC Music Store application, and now am making my own
I have to start a new Spring MVC project and I've been reading about
I'm making a call to an MVC controller method. The return type is FileStreamResult.
hii... I am making a view based application in which I have tabbar controller
I have been making a little toy web application in C# along the lines
I have tested a web application I have built using MVC 2 on local
I'm making a project with the Spring Framework and I use the Spring MVC
I created a web application in ASP.NET MVC and trying to call a controller
So I have this application in ASP MVC 3. My database has two tables:

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.