DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux
  • Graceful Shutdown: Spring Framework vs Golang Web Services
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4

Trending

  • Kubeflow: Driving Scalable and Intelligent Machine Learning Systems
  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  • Microsoft Azure Synapse Analytics: Scaling Hurdles and Limitations
  • Docker Model Runner: Streamlining AI Deployment for Developers
  1. DZone
  2. Coding
  3. Frameworks
  4. Don't Complicate Spring Controllers With Commotions

Don't Complicate Spring Controllers With Commotions

Don't complicate things.

By 
Mohammad Nadeem user avatar
Mohammad Nadeem
·
Apr. 26, 19 · Presentation
Likes (7)
Comment
Save
Tweet
Share
23.9K Views

Join the DZone community and get the full member experience.

Join For Free

@Controller Implementing Interface

You may get into the problem as described below

The mapped controller method class 'com.xyz.ABController'is not an instance of the actual controller bean instance 'com.sun.proxy.$Proxy108'.
If the controller requires proxying (e.g. due to @Transactional), please use class-based proxying.

So, it is better not to do the following:

Don’t

@Controller
public class SomeController implements SomeConcerenedInterface {

}


@Controller Extending Another @Controller

If the intent is to share the logic, it is better to do it with composition. In this case, the reason is you may end up exposing an endpoint unintentionally in  ChildController, which may be inherited from the  ParentController.

Further, that is the old way of doing things, before Annotations in Spring.

Don’t

@Controller
public class ParentController {

}

@Controller
public class ChildController extends ParentController {

}


Simple Controller

The best practice is to keep the @Controller class simple, as shown below (don’t implement the interface and don't extend another controller), @Controller should expose an endpoint, and the processing logic should be delegated to another abstraction. This way, logic can be shared across multiple  @Controllers if required.

Do

@Controller
public class SomeController {

}


@Controller Extending Another BasClass

This case should be avoided as much as possible. If possible, use composition here:

May Do

public class SomeBaseClassWithOutControllerAnnotation {
  //May contain methods Without @RequestMapping
}

**//Discouraged**
@Controller
public class SomeController extends SomeBaseClassWithOutControllerAnnotation  {

}


Happy controlling!

Spring Framework

Published at DZone with permission of Mohammad Nadeem, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux
  • Graceful Shutdown: Spring Framework vs Golang Web Services
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!