Table of Contents
Question content
Solution
Home Java Spring API controller

Spring API controller

Feb 10, 2024 pm 08:36 PM
spring framework

What php editor Zimo wants to introduce to you today is the Spring API controller. Controllers are a very important part when developing web applications. It is responsible for receiving requests from users and performing corresponding operations based on the content of the request. The Spring framework provides powerful API controllers that can help developers create and manage controllers more conveniently. By using Spring API controllers, developers can easily map requests to corresponding processing methods and handle request parameters, return results, etc. This powerful controller makes developing web applications more efficient and reliable.

Question content

I am developing this application and I would like some advice on the api hierarchy, I have a user class and a student class extending from user, and 3 others Classes (studentcode, studentconduite and studentpark) these 3 classes extend from student, the idea is that when the admin tries to add a new student, it will also ask to add a new user (the primary key is in the user class) and I want to check if the user has Exists in the studentservice class and calls a method that saves the user if the user does not exist yet, or checks if the user exists at the api level.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

//calling the method on StudentService:

@Service

public class StudentService {

 

    @Autowired

     StudentDao studentDao;

     UserDao userDao;

     UserService userService;

 

    public Student registerNewStudent(Student student) {

        User existingUser = userDao.findById(student.getUserName()).orElse(null);

        if (existingUser ==null) {

        userService.registerNewUser(student);

        }

        return studentDao.save(student);

    }

}

 

 

//or i check if the user exist in the api like this :

 

public class StudentController {

    @Autowired

    private StudentService studentService;

    private UserDao userDao;

    private UserController userController;

     

    @PostMapping({"/registerNewStudent"})

    public Student registerNewStudent(@RequestBody Student student) {

        User existingUser = userDao.findById(student.getUserName()).orElse(null);

 

        if (existingUser ==null) {

 

           

            userController.registerNewUser(student);

        }

        return studentService.registerNewStudent(student);

    }

     

 

}

 

 

 

//and this is registerNewUser methode from Userservice:

 

  public User registerNewUser(User user) {

        Role role = roleDao.findById("User").get();

        Set<Role> userRoles = new HashSet<>();

        userRoles.add(role);

        user.setRole(userRoles);

        user.setUserPassword(getEncodedPassword(user.getUserPassword()));

 

        return userDao.save(user);

    }

 

 

 

//and this is registerNewUser methode from UserController:

 

 @PostMapping({"/registerNewUser"})

 

 

    public User registerNewUser(@RequestBody User user) {

        return userService.registerNewUser(user);

    }

Copy after login

Solution

Controllers should not contain any business logic, they only delegate to appropriate service classes that encapsulate all logic. So define a service class and put all logic in it and call it from controller.

The above is the detailed content of Spring API controller. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use Spring Boot and Spring AI to build generative artificial intelligence applications Use Spring Boot and Spring AI to build generative artificial intelligence applications Apr 28, 2024 am 11:46 AM

As an industry leader, Spring+AI provides leading solutions for various industries through its powerful, flexible API and advanced functions. In this topic, we will delve into the application examples of Spring+AI in various fields. Each case will show how Spring+AI meets specific needs, achieves goals, and extends these LESSONSLEARNED to a wider range of applications. I hope this topic can inspire you to understand and utilize the infinite possibilities of Spring+AI more deeply. The Spring framework has a history of more than 20 years in the field of software development, and it has been 10 years since the Spring Boot 1.0 version was released. Now, no one can dispute that Spring

Modify RequestBody in spring gateway Modify RequestBody in spring gateway Feb 09, 2024 pm 07:15 PM

I want to modify the requestbody before routing it to the given uri. Modify the body based on the documentation I'm using org.springframework.cloud.gateway.filter.factory.rewrite.modifyrequestbodygatewayfilterfactory. When starting my server, the server fails to start with the following error reason: Element [spring.cloud.gateway.routes[0].filters[0].modifyrequestbody.class] is not bound. \n\nOperation:\

JAX-RS vs. Spring MVC: A battle between RESTful giants JAX-RS vs. Spring MVC: A battle between RESTful giants Feb 29, 2024 pm 05:16 PM

Introduction RESTful APIs have become an integral part of modern WEB applications. They provide a standardized approach to creating and using Web services, thereby improving portability, scalability, and ease of use. In the Java ecosystem, JAX-RS and springmvc are the two most popular frameworks for building RESTful APIs. This article will take an in-depth look at both frameworks, comparing their features, advantages, and disadvantages to help you make an informed decision. JAX-RS: JAX-RSAPI JAX-RS (JavaAPI for RESTful Web Services) is a standard JAX-RSAPI developed by JavaEE for developing REST

In-depth understanding of the architecture and working principles of the Spring framework In-depth understanding of the architecture and working principles of the Spring framework Jan 24, 2024 am 09:41 AM

An in-depth analysis of the architecture and working principles of the Spring framework Introduction: Spring is one of the most popular open source frameworks in the Java ecosystem. It not only provides a powerful set of container management and dependency injection functions, but also provides many other functions, such as transactions. Management, AOP, data access, etc. This article will provide an in-depth analysis of the architecture and working principles of the Spring framework, and explain related concepts through specific code examples. 1. Core concepts of the Spring framework 1.1IoC (Inversion of Control) Core of Spring

The Secret of Java JNDI and Spring Integration: Revealing the Seamless Cooperation of Java JNDI and Spring Framework The Secret of Java JNDI and Spring Integration: Revealing the Seamless Cooperation of Java JNDI and Spring Framework Feb 25, 2024 pm 01:10 PM

Advantages of integrating JavaJNDI with spring The integration of JavaJNDI with the Spring framework has many advantages, including: Simplifying the use of JNDI: Spring provides an abstraction layer that simplifies the use of JNDI without writing complex JNDI code. Centralized management of JNDI resources: Spring can centrally manage JNDI resources for easy search and management. Support multiple JNDI implementations: Spring supports multiple JNDI implementations, including JNDI, JNP, RMI, etc. Seamlessly integrate the Spring framework: Spring is very tightly integrated with JNDI and seamlessly integrates with the Spring framework. How to integrate JavaJNDI with Spring framework to integrate Ja

Optimizing program logging: Sharing tips on setting log4j log levels Optimizing program logging: Sharing tips on setting log4j log levels Feb 20, 2024 pm 02:27 PM

Optimizing program logging: Tips for setting log4j log levels Summary: Program logging plays a key role in troubleshooting, performance tuning, and system monitoring. This article will share tips on setting log4j log levels, including how to set different levels of logs and how to illustrate the setting process through code examples. Introduction: In software development, logging is a very important task. By recording key information during the running process of the program, it can help developers find out the cause of the problem and perform performance optimization and system monitoring.

Detailed explanation of Oracle database connection methods Detailed explanation of Oracle database connection methods Mar 08, 2024 am 08:45 AM

Detailed explanation of Oracle database connection methods In application development, database connection is a very important link, which carries the data interaction between the application and the database. Oracle database is a relational database management system with powerful functions and stable performance. In actual development, we need to be proficient in different connection methods to interact with Oracle database. This article will introduce in detail several common connection methods of Oracle database and provide corresponding code examples to help readers better understand and apply

Application of Java reflection mechanism in Spring framework? Application of Java reflection mechanism in Spring framework? Apr 15, 2024 pm 02:03 PM

Java reflection mechanism is widely used in Spring framework for the following aspects: Dependency injection: instantiating beans and injecting dependencies through reflection. Type conversion: Convert request parameters to method parameter types. Persistence framework integration: mapping entity classes and database tables. AspectJ support: intercepting method calls and enhancing code behavior. Dynamic Proxy: Create proxy objects to enhance the behavior of the original object.