use of org.springframework.web.bind.annotation.RequestMethod in project spring-framework by spring-projects.
the class RequestMappingHandlerMappingTests method assertComposedAnnotationMapping.
private RequestMappingInfo assertComposedAnnotationMapping(String methodName, String path, RequestMethod requestMethod) throws Exception {
Class<?> clazz = ComposedAnnotationController.class;
Method method = clazz.getMethod(methodName);
RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, clazz);
assertNotNull(info);
Set<String> paths = info.getPatternsCondition().getPatterns();
assertEquals(1, paths.size());
assertEquals(path, paths.iterator().next());
Set<RequestMethod> methods = info.getMethodsCondition().getMethods();
assertEquals(1, methods.size());
assertEquals(requestMethod, methods.iterator().next());
return info;
}
use of org.springframework.web.bind.annotation.RequestMethod in project nikita-noark5-core by HiOA-ABI.
the class AfterApplicationStartup method afterApplicationStarts.
/**
* afterApplicationStarts, go through list of endpoints and make a list of
* endpoints and the HTTP methods they support. Really this should be
* handled by Spring. I do not know why I couldn't get spring to handle
* this but we need to get spring to handle these things ... not me!!!
*
* Also create a list of Norwegian names to english names for handling OData
*/
public void afterApplicationStarts() {
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMapping.getHandlerMethods().entrySet()) {
RequestMappingInfo requestMappingInfo = entry.getKey();
// Assuming there is always a non-null value
String servletPaths = requestMappingInfo.getPatternsCondition().toString();
// servletPath starts with "[" and ends with "]". Removing them if they are there
if (true == servletPaths.startsWith("[")) {
servletPaths = servletPaths.substring(1);
}
if (true == servletPaths.endsWith("]")) {
servletPaths = servletPaths.substring(0, servletPaths.length() - 1);
}
String[] servletPathList = servletPaths.split("\\s+");
for (String servletPath : servletPathList) {
if (servletPath != null && false == servletPath.contains("|")) {
// This is done to be consist on a lookup
if (false == servletPath.endsWith("/")) {
servletPath += SLASH;
}
Set<RequestMethod> httpMethodRequests = requestMappingInfo.getMethodsCondition().getMethods();
if (null != httpMethodRequests && null != servletPath) {
// RequestMethod and HTTPMethod are different types, have to convert them here
Set<HttpMethod> httpMethods = new TreeSet<>();
for (RequestMethod requestMethod : httpMethodRequests) {
if (requestMethod.equals(RequestMethod.GET)) {
httpMethods.add(HttpMethod.GET);
} else if (requestMethod.equals(RequestMethod.DELETE)) {
httpMethods.add(HttpMethod.DELETE);
} else if (requestMethod.equals(RequestMethod.OPTIONS)) {
httpMethods.add(HttpMethod.OPTIONS);
} else if (requestMethod.equals(RequestMethod.HEAD)) {
httpMethods.add(HttpMethod.HEAD);
} else if (requestMethod.equals(RequestMethod.PATCH)) {
httpMethods.add(HttpMethod.PATCH);
} else if (requestMethod.equals(RequestMethod.POST)) {
httpMethods.add(HttpMethod.POST);
} else if (requestMethod.equals(RequestMethod.PUT)) {
httpMethods.add(HttpMethod.PUT);
} else if (requestMethod.equals(RequestMethod.TRACE)) {
httpMethods.add(HttpMethod.TRACE);
}
}
out.println("Adding " + servletPath + " methods " + httpMethods);
CommonUtils.WebUtils.addRequestToMethodMap(servletPath, httpMethods);
} else {
logger.warn("Missing HTTP Methods for the following servletPath [" + servletPath + "]");
}
}
}
}
populateTranslatedNames();
}
use of org.springframework.web.bind.annotation.RequestMethod in project judge by zjnu-acm.
the class MockGenerator method test.
@Test
public void test() throws IOException {
Gson gson = new Gson();
Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
Map<Class<?>, List<Info>> map = handlerMethods.entrySet().stream().map(entry -> new Info(entry.getValue(), entry.getKey())).collect(Collectors.groupingBy(info -> info.getHandlerMethod().getMethod().getDeclaringClass()));
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw, true);
for (Map.Entry<Class<?>, List<Info>> entry : map.entrySet()) {
Class<?> key = entry.getKey();
List<Info> value = entry.getValue();
if (!accept(key, value)) {
continue;
}
value.sort(Comparator.comparing(info -> info.getHandlerMethod().getMethod(), Comparator.comparing(method -> method.getDeclaringClass().getName().replace(".", "/") + "." + method.getName() + ":" + org.springframework.asm.Type.getMethodDescriptor(method), toMethodComparator(key))));
TestClass testClass = new TestClass(key, "@AutoConfigureMockMvc", "@RunWith(SpringRunner.class)", "@Slf4j", "@SpringBootTest(classes = " + mainClass.getSimpleName() + ".class)", "@Transactional", "@WebAppConfiguration");
testClass.addImport(mainClass);
testClass.addImport(AutoConfigureMockMvc.class);
testClass.addImport(RunWith.class);
testClass.addImport(SpringRunner.class);
testClass.addImport(Slf4j.class);
testClass.addImport(SpringBootTest.class);
testClass.addImport(Transactional.class);
testClass.addImport(WebAppConfiguration.class);
testClass.addImport(Autowired.class);
testClass.addField(MockMvc.class, "mvc", "@Autowired");
for (Info info : value) {
RequestMappingInfo requestMappingInfo = info.getRequestMappingInfo();
Set<RequestMethod> requestMethods = requestMappingInfo.getMethodsCondition().getMethods();
String requestMethod = requestMethods.isEmpty() ? "post" : requestMethods.iterator().next().toString().toLowerCase();
HandlerMethod handlerMethod = info.getHandlerMethod();
String url = gson.toJson(requestMappingInfo.getPatternsCondition().getPatterns().iterator().next());
generate(key, requestMappingInfo, handlerMethod, url, testClass, requestMethod);
}
testClass.write(out);
Path to = outputDir.resolve(key.getName().replace(".", "/") + "Test.java");
Files.createDirectories(to.getParent());
Files.write(to, sw.toString().replace("\t", " ").getBytes(StandardCharsets.UTF_8));
sw.getBuffer().setLength(0);
}
}
use of org.springframework.web.bind.annotation.RequestMethod in project RestyPass by darren-fu.
the class RequestMappingProcessor method process.
@Override
public boolean process(RestyRequestTemplate requestTemplate, Annotation annotation) {
RequestMapping methodMapping = ANNOTATION.cast(annotation);
// HTTP Method
RequestMethod[] methods = methodMapping.method();
if (methods.length == 0) {
methods = new RequestMethod[] { RequestMethod.GET };
}
checkOne(methods, "method");
requestTemplate.setHttpMethod(methods[0].name());
// path
checkAtMostOne(methodMapping.value(), "value");
if (methodMapping.value().length > 0) {
String pathValue = emptyToNull(methodMapping.value()[0]);
if (pathValue != null) {
pathValue = resolve(pathValue);
// Append path from @RequestMapping if value is present on httpMethod
if (!pathValue.startsWith("/")) {
pathValue = "/" + pathValue;
}
requestTemplate.setMethodUrl(pathValue);
}
}
// produces #不需要解析consumes, 无视接口返回的content-type,只处理application/json
// parseProduces(requestTemplate, method, methodMapping);
// consumes #不需要解析consumes, 无视接口所需的content-type,只提交application/json
// parseConsumes(requestTemplate, method, methodMapping);
// params
parseParams(requestTemplate, null, methodMapping);
// headers
parseHeaders(requestTemplate, null, methodMapping);
return false;
}
use of org.springframework.web.bind.annotation.RequestMethod in project spring-framework by spring-projects.
the class RequestMethodsRequestConditionTests method getMatchingConditionWithEmptyConditions.
@Test
public void getMatchingConditionWithEmptyConditions() {
RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition();
for (RequestMethod method : RequestMethod.values()) {
if (method != OPTIONS) {
HttpServletRequest request = new MockHttpServletRequest(method.name(), "");
assertThat(condition.getMatchingCondition(request)).isNotNull();
}
}
testNoMatch(condition, OPTIONS);
}
Aggregations