Package org.springbyexample.mvc.bind.annotation

Examples of org.springbyexample.mvc.bind.annotation.RestResource


                    throw new RuntimeException("Error loading interface class.", e);
                } catch (LinkageError e) {
                    throw new RuntimeException("Error loading interface class.", e);
                }
               
                RestResource restResource = AnnotationUtils.findAnnotation(marshallingServiceClass, RestResource.class);

                if (restResource.exportClient()) {
                    registerClient(registry, marshallingServiceClass);
                }
            }
        }
    }
View Full Code Here


    public Object getBean() throws BeansException {
        logger.info("########## {} ######", marshallingServiceClass);

        if (bean == null) {
            RestResource restResource = AnnotationUtils.findAnnotation(marshallingServiceClass, RestResource.class);
            Class<?> serviceClass = restResource.service();

            Map<Method, ClientRequestHandlerInfo> methodRequestHandlers = new HashMap<Method, ClientRequestHandlerInfo>();

            Set<Method> methods = HandlerMethodSelector.selectMethods(marshallingServiceClass, new MethodFilter() {
                @Override
                public boolean matches(Method method) {
                    return (AnnotationUtils.findAnnotation(method, RequestMapping.class) != null);
                }
            });

            for (Method interfaceMethod : methods) {
                Method method = BridgeMethodResolver.findBridgedMethod(interfaceMethod);
                Class<?> responseClazz = method.getReturnType();

                RequestMapping requestMapping = AnnotationUtils.findAnnotation(method, RequestMapping.class);
                RestRequestResource restRequestResource = AnnotationUtils.findAnnotation(method, RestRequestResource.class);
                boolean export = (restRequestResource != null ? restRequestResource.export() : true);

                // if the method declaring class is in the parent, get the return type from the service or converter (because of erasure)
                if (!marshallingServiceClass.equals(method.getDeclaringClass())) {
                    String restRequestResourceMethodName = (restRequestResource != null ? restRequestResource.methodName() : null);
                    String methodName = (StringUtils.hasText(restRequestResourceMethodName) ? restRequestResourceMethodName : method.getName());
                    Class<?>[] paramTypes = method.getParameterTypes();

                    try {
                        Method serviceMethod = ReflectionUtils.findMethod(serviceClass, methodName, paramTypes);
                        responseClazz = serviceMethod.getReturnType();
                    } catch(IllegalStateException e) {
                    }
                }

                if (export) {
                    boolean relative = (restRequestResource != null ? restRequestResource.relative() : true);

                    // just get first param if more than one
                    StringBuilder uri = new StringBuilder();

                    if (relative) {
                        for (String pattern : requestMapping.value()) {
                            // add REST resource path prefix to URI,
                            // if relative path is just '/' add an empty string
                            uri.append(restResource.path());
                            uri.append((!"/".equals(pattern) ? pattern : ""));

                            break;
                        }
                    } else {
                        uri.append(requestMapping.value()[0]);
                    }

                    String[] params = requestMapping.params();

                    if (params != null && params.length > 0) {
                        uri.append("?");

                        for (int i = 0; i < params.length; i++) {
                            String param = params[i];

                            uri.append(param);
                            uri.append("={");
                            uri.append(param);
                            uri.append("}");

                            if (i < (params.length - 1)) {
                                uri.append("&");
                            }
                        }
                    }

                    RequestMethod requestMethod = requestMapping.method()[0];

                    if (RequestMethod.POST.equals(requestMethod) || RequestMethod.PUT.equals(requestMethod)
                            || RequestMethod.DELETE.equals(requestMethod)) {
                        responseClazz = restResource.responseClass();
                    }
//                    else if (RequestMethod.DELETE.equals(requestMethod)) {
//                        responseClazz = ResponseResult.class;
//                    }
View Full Code Here

     * Register handler.
     */
    private void registerHandler(Class<?> marshallingServiceClass, Method method) {
        ApplicationContext ctx = getApplicationContext();

        RestResource restResource = AnnotationUtils.findAnnotation(marshallingServiceClass, RestResource.class);
        Class<?> serviceClass = restResource.service();

        RestRequestResource restRequestResource = AnnotationUtils.findAnnotation(method, RestRequestResource.class);
        boolean export = (restRequestResource != null ? restRequestResource.export() : true);
        boolean relative = (restRequestResource != null ? restRequestResource.relative() : true);

        if (export) {
            Class<?> handlerServiceClass = serviceClass;
            String methodName = method.getName();
            Class<?>[] paramTypes = method.getParameterTypes();

            if (restRequestResource != null) {
                // explicit service specified
                if (restRequestResource.service() != ServiceValueConstants.DEFAULT_SERVICE_CLASS) {
                    handlerServiceClass = restRequestResource.service();
                }

                // explicit method name specified
                if (StringUtils.hasText(restRequestResource.methodName())) {
                    methodName = restRequestResource.methodName();
                }
            }

            Object handler = ctx.getBean(handlerServiceClass);
            Method serviceMethod = ClassUtils.getMethod(handlerServiceClass, methodName, paramTypes);
            RequestMappingInfo mapping = getMappingForMethod(method, marshallingServiceClass);

            if (relative) {
                List<String> patterns = new ArrayList<String>();

                for (String pattern : mapping.getPatternsCondition().getPatterns()) {
                    // add REST resource path prefix to URI,
                    // if relative path is just '/' add an empty string
                    patterns.add(restResource.path() + (!"/".equals(pattern) ? pattern : ""));
                }

                // create a new mapping based on the patterns (patterns are unmodifiable in existing RequestMappingInfo)
                mapping = new RequestMappingInfo(
                            new PatternsRequestCondition(patterns.toArray(ArrayUtils.EMPTY_STRING_ARRAY),
View Full Code Here

TOP

Related Classes of org.springbyexample.mvc.bind.annotation.RestResource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.