Example usage for org.springframework.web.method HandlerMethod HandlerMethod

List of usage examples for org.springframework.web.method HandlerMethod HandlerMethod

Introduction

In this page you can find the example usage for org.springframework.web.method HandlerMethod HandlerMethod.

Prototype

public HandlerMethod(String beanName, BeanFactory beanFactory, Method method) 

Source Link

Document

Create an instance from a bean name, a method, and a BeanFactory .

Usage

From source file:learning.candystore.controller.CandyActionServerSideTest.java

@Test
@Ignore//  w  w w  . j av  a  2  s .  c  om
public void registerStudent() {
    request.setRequestURI("/student");
    request.setMethod(HttpMethod.POST.name());
    request.setContentType("application/json");

    String json = "{\"id\":\"2\",\"name\":\"tenjin\",\"age\":\"23\"}";
    request.setContent(json.getBytes());
    Class<?>[] parameterTypes = new Class<?>[] { Candy.class };
    ModelAndView mv = null;
    try {
        mv = handlerAdapter.handle(request, response,
                new HandlerMethod(candyAction, "registerStudent", parameterTypes));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:nd.dev.framework.basedemo.controllers.StudentActionServerSideTest.java

@Test
@Ignore/*from ww  w . j av  a  2s .  c  om*/
public void registerStudent() {
    request.setRequestURI("/student");
    request.setMethod(HttpMethod.POST.name());
    request.setContentType("application/json");

    String json = "{\"id\":\"2\",\"name\":\"tenjin\",\"age\":\"23\"}";
    request.setContent(json.getBytes());
    Class<?>[] parameterTypes = new Class<?>[] { Student.class };
    ModelAndView mv = null;
    try {
        mv = handlerAdapter.handle(request, response,
                new HandlerMethod(studentAction, "registerStudent", parameterTypes));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:capital.scalable.restdocs.payload.JacksonRequestFieldSnippetTest.java

@Test
public void simpleRequest() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
            .withFieldVisibility(JsonAutoDetect.Visibility.ANY));

    HandlerMethod handlerMethod = new HandlerMethod(new TestResource(), "addItem", Item.class);
    JavadocReader javadocReader = mock(JavadocReader.class);
    when(javadocReader.resolveFieldComment(Item.class, "field1")).thenReturn("A string");
    when(javadocReader.resolveFieldComment(Item.class, "field2"))
            .thenReturn("An integer<br>\n Very important\n <p>\n field");

    ConstraintReader constraintReader = mock(ConstraintReader.class);
    when(constraintReader.isMandatory(NotBlank.class)).thenReturn(true);
    when(constraintReader.getOptionalMessages(Item.class, "field1")).thenReturn(singletonList("false"));
    when(constraintReader.getConstraintMessages(Item.class, "field2"))
            .thenReturn(singletonList("A constraint"));

    this.snippet.expectRequestFields()
            .withContents(tableWithHeader("Path", "Type", "Optional", "Description")
                    .row("field1", "String", "false", "A string.")
                    .row("field2", "Integer", "true", "An integer" + lineBreak() + "Very important"
                            + lineBreak() + lineBreak() + "field." + lineBreak() + "A constraint."));

    new JacksonRequestFieldSnippet()
            .document(operationBuilder.attribute(HandlerMethod.class.getName(), handlerMethod)
                    .attribute(ObjectMapper.class.getName(), mapper)
                    .attribute(JavadocReader.class.getName(), javadocReader)
                    .attribute(ConstraintReader.class.getName(), constraintReader).request("http://localhost")
                    .content("{\"field1\":\"test\"}").build());
}

From source file:rugal.sample.controller.StudentActionServerSideTest.java

@Test
//    @Ignore/*from  w w w.  ja  va2  s . com*/
public void getAddress() {
    request.setMethod(HttpMethod.GET.name());
    request.setRequestURI("/student/{id}");
    HashMap<String, String> pathVariablesMap = new HashMap<>(1);
    pathVariablesMap.put("id", "3");
    request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, pathVariablesMap);
    Class<?>[] parameterTypes = new Class<?>[] { Integer.class };
    ModelAndView mv = null;
    try {
        mv = handlerAdapter.handle(request, response,
                new HandlerMethod(studentAction, "retrieve", parameterTypes));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:learning.candystore.controller.CandyActionServerSideTest.java

@Test
@Ignore//from   ww  w. jav  a 2 s.c  o m
public void getAddress() {
    request.setMethod(HttpMethod.GET.name());
    request.setRequestURI("/student/{id}");
    HashMap<String, String> pathVariablesMap = new HashMap<>(1);
    pathVariablesMap.put("id", "1");
    request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, pathVariablesMap);
    Class<?>[] parameterTypes = new Class<?>[] { Integer.class };
    ModelAndView mv = null;
    try {
        mv = handlerAdapter.handle(request, response,
                new HandlerMethod(candyAction, "retrieve", parameterTypes));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:nd.dev.framework.basedemo.controllers.StudentActionServerSideTest.java

public void getAddress() {
    request.setMethod(HttpMethod.GET.name());
    request.setRequestURI("/student/{id}");
    HashMap<String, String> pathVariablesMap = new HashMap<>(1);
    pathVariablesMap.put("id", "1");
    request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, pathVariablesMap);
    Class<?>[] parameterTypes = new Class<?>[] { Integer.class };
    ModelAndView mv = null;//from w ww  .ja  v a2 s.co m
    try {
        mv = handlerAdapter.handle(request, response,
                new HandlerMethod(studentAction, "retrieve", parameterTypes));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:capital.scalable.restdocs.payload.JacksonRequestFieldSnippetTest.java

@Test
public void listRequest() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
            .withFieldVisibility(JsonAutoDetect.Visibility.ANY));

    HandlerMethod handlerMethod = new HandlerMethod(new TestResource(), "addItems", List.class);
    JavadocReader javadocReader = mock(JavadocReader.class);
    when(javadocReader.resolveFieldComment(Item.class, "field1")).thenReturn("A string");
    when(javadocReader.resolveFieldComment(Item.class, "field2")).thenReturn("An integer");

    this.snippet.expectRequestFields()
            .withContents(tableWithHeader("Path", "Type", "Optional", "Description")
                    .row("[].field1", "String", "true", "A string.")
                    .row("[].field2", "Integer", "true", "An integer."));

    new JacksonRequestFieldSnippet()
            .document(operationBuilder.attribute(HandlerMethod.class.getName(), handlerMethod)
                    .attribute(ObjectMapper.class.getName(), mapper)
                    .attribute(JavadocReader.class.getName(), javadocReader)
                    .attribute(ConstraintReader.class.getName(), mock(ConstraintReader.class))
                    .request("http://localhost").content("{\"field1\":\"test\"}").build());
}

From source file:ch.ralscha.extdirectspring.util.MethodInfo.java

public MethodInfo(Class<?> clazz, ApplicationContext context, String beanName, Method method) {

    ExtDirectMethod extDirectMethodAnnotation = AnnotationUtils.findAnnotation(method, ExtDirectMethod.class);

    this.type = extDirectMethodAnnotation.value();

    if (extDirectMethodAnnotation.jsonView() != ExtDirectMethod.NoJsonView.class) {
        this.jsonView = extDirectMethodAnnotation.jsonView();
    } else {/*from  w  w  w .java  2 s.co  m*/
        this.jsonView = null;
    }

    if (StringUtils.hasText(extDirectMethodAnnotation.group())) {
        this.group = extDirectMethodAnnotation.group().trim();
    } else {
        this.group = null;
    }

    this.synchronizeOnSession = extDirectMethodAnnotation.synchronizeOnSession();
    this.streamResponse = extDirectMethodAnnotation.streamResponse();

    if (this.type != ExtDirectMethodType.FORM_POST) {
        this.method = method;
        this.parameters = buildParameterList(clazz, method);

        this.collectionType = extDirectMethodAnnotation.entryClass() == Object.class ? null
                : extDirectMethodAnnotation.entryClass();

        if (this.collectionType == null) {
            for (ParameterInfo parameter : this.parameters) {
                Class<?> collType = parameter.getCollectionType();
                if (collType != null) {
                    this.collectionType = collType;
                    break;
                }
            }
        }
    } else {
        if (method.getReturnType().equals(Void.TYPE)) {

            RequestMapping methodAnnotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
            RequestMapping classAnnotation = AnnotationUtils.findAnnotation(clazz, RequestMapping.class);

            String path = null;
            if (hasValue(classAnnotation)) {
                path = classAnnotation.value()[0];
            }

            if (hasValue(methodAnnotation)) {
                String methodPath = methodAnnotation.value()[0];
                if (path != null) {
                    path = path + methodPath;
                } else {
                    path = methodPath;
                }
            }

            if (path != null) {
                if (path.charAt(0) == '/' && path.length() > 1) {
                    path = path.substring(1, path.length());
                }
                this.forwardPath = "forward:" + path;
            }
        } else {
            this.handlerMethod = new HandlerMethod(beanName, context, method).createWithResolvedBean();
        }
    }

    switch (this.type) {
    case SIMPLE:
        int paramLength = 0;
        for (ParameterInfo parameter : this.parameters) {
            if (parameter.isClientParameter()) {
                paramLength++;
            }
        }
        this.action = Action.create(method.getName(), paramLength, extDirectMethodAnnotation.batched());
        break;
    case SIMPLE_NAMED:
        int noOfClientParameters = 0;
        Class<?> parameterType = null;

        List<String> parameterNames = new ArrayList<String>();
        for (ParameterInfo parameter : this.parameters) {
            if (parameter.isClientParameter()) {
                noOfClientParameters++;
                parameterType = parameter.getType();
                parameterNames.add(parameter.getName());
            }
        }

        if (noOfClientParameters == 1 && Map.class.isAssignableFrom(parameterType)) {
            this.action = Action.createNamed(method.getName(), Collections.<String>emptyList(), Boolean.FALSE,
                    extDirectMethodAnnotation.batched());
        } else {
            this.action = Action.createNamed(method.getName(), Collections.unmodifiableList(parameterNames),
                    null, extDirectMethodAnnotation.batched());
        }
        break;
    case FORM_LOAD:
        this.action = Action.create(method.getName(), 1, extDirectMethodAnnotation.batched());
        break;
    case STORE_READ:
    case STORE_MODIFY:
    case TREE_LOAD:
        List<String> metadataParams = new ArrayList<String>();
        for (ParameterInfo parameter : this.parameters) {
            if (parameter.hasMetadataParamAnnotation()) {
                metadataParams.add(parameter.getName());
            }
        }
        this.action = Action.createTreeLoad(method.getName(), 1, metadataParams,
                extDirectMethodAnnotation.batched());
        break;
    case FORM_POST:
        this.action = Action.createFormHandler(method.getName(), 0);
        break;
    case FORM_POST_JSON:
        this.action = Action.create(method.getName(), 1, extDirectMethodAnnotation.batched());
        break;
    case POLL:
        this.pollingProvider = new PollingProvider(beanName, method.getName(),
                extDirectMethodAnnotation.event());
        break;
    default:
        throw new IllegalStateException("ExtDirectMethodType: " + this.type + " does not exists");
    }

    this.action = extractDocumentationAnnotations(extDirectMethodAnnotation.documentation());

}

From source file:capital.scalable.restdocs.payload.JacksonRequestFieldSnippetTest.java

@Test
public void jsonSubTypesRequest() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
            .withFieldVisibility(JsonAutoDetect.Visibility.ANY));

    HandlerMethod handlerMethod = new HandlerMethod(new TestResource(), "addSubItem", ParentItem.class);
    JavadocReader javadocReader = mock(JavadocReader.class);
    when(javadocReader.resolveFieldComment(ParentItem.class, "type")).thenReturn("A type");
    when(javadocReader.resolveFieldComment(ParentItem.class, "commonField")).thenReturn("A common field");
    when(javadocReader.resolveFieldComment(SubItem1.class, "subItem1Field")).thenReturn("A sub item 1 field");
    when(javadocReader.resolveFieldComment(SubItem2.class, "subItem2Field")).thenReturn("A sub item 2 field");

    ConstraintReader constraintReader = mock(ConstraintReader.class);

    this.snippet.expectRequestFields().withContents(tableWithHeader("Path", "Type", "Optional", "Description")
            .row("type", "String", "true", "A type.").row("commonField", "String", "true", "A common field.")
            .row("subItem1Field", "Boolean", "true", "A sub item 1 field.")
            .row("subItem2Field", "Integer", "true", "A sub item 2 field."));

    new JacksonRequestFieldSnippet()
            .document(operationBuilder.attribute(HandlerMethod.class.getName(), handlerMethod)
                    .attribute(ObjectMapper.class.getName(), mapper)
                    .attribute(JavadocReader.class.getName(), javadocReader)
                    .attribute(ConstraintReader.class.getName(), constraintReader).request("http://localhost")
                    .content("{\"type\":\"1\"}").build());
}