mercredi 14 septembre 2022

How to implement the GET request using ServiceLocatorFactoryBean ( Factory Method design Pattern)

I thank you ahead for your time to read my request. I'm new to the Spring Service Locator Factory Method design Pattern and I don't understand the approach behind it. However I followed a turtorial and have been able to implement the Post request for my user registratio spring maven application. My src/main/java folder cointains this five packages:

  1. Config
  2. Controller
  3. Model
  4. Registry
  5. Service

The Config package is to centralize the creation of users and its java class is as bellow: package com.nidservices.yekoregistration.config;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.config.ServiceLocatorFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.nidservices.yekoregistration.registry.ServiceRegistry;

@Configuration
public class UserConfig {
  @Bean
  public FactoryBean<?> factoryBean() {
      final ServiceLocatorFactoryBean bean = new ServiceLocatorFactoryBean();
      bean.setServiceLocatorInterface(ServiceRegistry.class);
    return bean;
  }
}

The Registry package is to adapt the service base on the type of entity to create and is as bellow:

package com.nidservices.yekoregistration.registry;

public interface AdapterService<T> {
  public void process(T request);
}



 package com.nidservices.yekoregistration.registry;

public interface ServiceRegistry {
  public <T> AdapterService<T> getService(String serviceName);
}

The Service package contains the different types of entity that inherit the User Model and the User Model is as bellow:

public class User implements Serializable {
    private UUID id;
    private String userIdentifier;
    private String userType;
    
    
    public String getUserIdentifier() {
        return userIdentifier;
    }
    public void setUserIdentifier(String userIdentifier) {
        this.userIdentifier = userIdentifier;
    }
    public String getUserType() {
        return userType;
    }
    public void setUserType(String userType) {
        this.userType = userType;
    }
    
    
    @Override
    public String toString() {
        return "User [userIdentifier=" + userIdentifier + ", UserType=" + userType + "]";
        
    }

}

And the Post Request defined in the Controller is as bellow:

package com.nidservices.yekoregistration.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nidservices.yekoregistration.model.User;
import com.nidservices.yekoregistration.registry.ServiceRegistry;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private ServiceRegistry serviceRegistry;
    
    @PostMapping
    public void processStudentDetails(@RequestBody User user) {
        serviceRegistry.getService(user.getUserType()).process(user);
    }
}

Now I'm struggling to make the GET Request to get all created users. I'm used with the DAO design pattern and very new with the concept behind ServiceLocatorFactoryBean. I appreciate your help to help me implement my CRUD endpoints using ServiceLocatorFactoryBean. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire