I have one controller method(in real project it SOAP service's method):
@Slf4j
@RestController
public class ControllerDispatcher {
@PostMapping("request")
public void sendGenericRequest(@RequestBody MainRequest mainRequest) {}
}
In this method I receive MainRequest
class. This class is wrapper(ExternalRequest). It contains 3 internal requests:
@Data
public class MainRequest {
enum Type {FIRST, SECOND, LAST}
private Type type; //type of request
private String request; //internal request in String format.
}
I need logic like spring dispather-servlet(something similar) and having caught the request to throw it in the desired method. I create Handler
with custom annotation:
@InternalHandlerAnnotation
public class InternalRequestHandler {
public void firstInternalrequest(FirstInternalRequest firstInternalRequest){
}
public void secondInternalrequest(SecondInternalRequest secondInternalRequest){
}
public void lastInternalrequest(LastInternalRequest lastInternalRequest){
}
}
I want hadle MainRequest
, unmarshal InternalRequest
from MainRequest
to class and redirect to concrete hadler's method.
It is 3 internal requests with I can receive:
@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;
}
Now I need mechanism for detect and redirect internal request to methods of handler class. I have resolcer:
@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
}
}
}
}
}
}
I can detect bean with my annotation. Get methods and in each method get Parameter. but how to be next do not understand. How can I convert String from MainRequets
to this type and cal method in handler?
Aucun commentaire:
Enregistrer un commentaire