У меня есть один метод контроллера (в реальном проекте это метод службы SOAP):
@Slf4j
@RestController
public class ControllerDispatcher {
@PostMapping("request")
public void sendGenericRequest(@RequestBody MainRequest mainRequest) {}
}
В этом методе я получаю MainRequest
класс.Этот класс является оберткой (ExternalRequest).Он содержит 3 внутренних запроса:
@Data
public class MainRequest {
enum Type {FIRST, SECOND, LAST}
private Type type; //type of request
private String request; //internal request in String format.
}
Мне нужна логика наподобие весеннего диспетчера-сервлета (что-то похожее) и перехвата запроса, чтобы бросить его в нужный метод.Я создаю Handler
с пользовательской аннотацией:
@InternalHandlerAnnotation
public class InternalRequestHandler {
public void firstInternalrequest(FirstInternalRequest firstInternalRequest){
}
public void secondInternalrequest(SecondInternalRequest secondInternalRequest){
}
public void lastInternalrequest(LastInternalRequest lastInternalRequest){
}
}
Я хочу, чтобы hadle MainRequest
, unmarshal InternalRequest
из MainRequest
в класс и перенаправил на метод конкретного хадлера.
Это 3 внутренних запроса, которые я могу получить:
@Data
public class FirstInternalRequest {
private Long id;
private String name;
private int age;
}
@Data
public class SecondInternalRequest {
private Long id;
private String description;
}
@Data
public class LastInternalRequest {
private Long id;
private String hz;
private int hz2;
}
Теперь мне нужен механизм для обнаружения и перенаправления внутреннего запроса в методы класса обработчика.У меня есть преобразователь:
@Component
public class HandlersResolver {
private final ApplicationContext context;
private Map<String, Object> beansWithAnnotation;
public HandlersResolver(ApplicationContext context) {
this.context = context;
}
@PostConstruct
public void init() {
beansWithAnnotation = context.getBeansWithAnnotation(InternalHandlerAnnotation.class);
initHandler();
}
private void initHandler() {
for (Map.Entry<String, Object> entry : beansWithAnnotation.entrySet()) {
Annotation[] annotations = entry.getValue().getClass().getAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof InternalHandlerAnnotation) {
Object beanWithMyAnnotation = entry.getValue();
Method[] declaredMethods = beanWithMyAnnotation.getClass().getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
Parameter[] parameters = declaredMethod.getParameters();
Parameter parameter = parameters[0];
Class<?> type = parameter.getType();
//convert String to type
}
}
}
}
}
}
Я могу обнаружить бин с помощью моей аннотации.Получить методы и в каждом методе получить параметр.но как быть дальше не понимаю.Как я могу преобразовать String из MainRequets
в этот тип и метод cal в обработчике?