- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有两个参数 long 和 String 的 Spring Controller :
@RequestMapping(value = "/webpage")
@Controller
public class WebpageContentController {
//...
@RequestMapping(value = "{webpageId}/{webpageAddress}", method = RequestMethod.GET)
public String contentWebpageById(@PathVariable long webpageId, @PathVariable String webpageAddress) {
System.out.println("webpageId=" + webpageId);
System.out.println("webpageAddress=" + webpageAddress);
//...
}
//...
如果我这样调用它:
http://localhost:8080/webarch/webpage/1/blahblah
一切都很好:
webpageId=1
webpageAddress=blahblah
但是如果我用斜杠传递字符串参数(在本例中是 URL 地址):
http://localhost:8080/webarch/webpage/1/https://en.wikipedia.org/wiki/Main_Page
我得到一个错误:
org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/webarch/webpage/1/https://en.wikipedia.org/wiki/Main_Page] in DispatcherServlet with name 'appServlet'
如何传递这样的参数?
最佳答案
这个错误是由 spring Controller 映射引起的,当 spring 看到这样的 url 时
http://localhost:8080/webarch/webpage/1/https://en.wikipedia.org/wiki/Main_Page
它“不知道”“https://en.wikipedia.org/wiki/Main_Page” ' 应该作为参数映射到“{webpageId}/{webpageAddress}”映射,因为每个斜杠都被解释为更深层次的 Controller 方法映射。它寻找像 (webpage/1/http:{anotherMapping}/wiki{anotherMapping}/Main_Page{anotherMapping})
这样的 Controller 方法映射,这种映射显然不是由“{webpageId}/”处理的{网页地址}”
编辑
根据您的评论,您可以尝试这样的操作
@RequestMapping(value = "/{webpageId}/**", method = RequestMethod.GET)
public String contentWebpageById(HttpServletRequest request) {
String pattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
String extractedPathParam = pathMatcher.extractPathWithinPattern(pattern, request.getServletPath());
extractedPathParam = extractedPathParam.replace("http:/", "http://");
extractedPathParam = extractedPathParam.replace("https:/", "https://");
//do whatever you want with parsed string..
}
使用 Spring 4.2.1
SomeParsing 应该使用一些正则表达式来仅提取 URL“变量”
关于java - 带有包含 URL 的字符串参数的 RequestMapping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33662690/
如何在Spring中忽略类级别@RequestMapping("/home")并直接调用方法级别@RequestMapping("/users")? @Controller @RequestMappi
让我们假设以下 Controller : @RestController public class MyController { @RequestMapping(method = GET, pat
我在使用 Spring Boot 时遇到了一些问题。 当我使用 @RequestMapping(value = "/home") 时,一切正常。但是当我使用 @RequestMapping(name
我正在尝试创建一个restful API(使用Spring Boot v2.0.0.Release),我希望有一个端点,但我希望有两种可能的用途: GET /time - outputs curren
我已经被这个错误困扰了几个小时。。我的pom.xml。应用程序未启动。所有的Spring框架依赖于相同的版本,但仍然得到相同的错误。。更新。MVN依赖的结果:树。看起来这里一切都很好。
我有以下三个 REST API 方法: @RequestMapping(value = "/{name1}", method = RequestMethod.GET) public Objec
我正在尝试为 spring @RequestMapping 注释创建值属性以像这样映射 url /educationDistrict/308/action/resetAddressesForYear/
如何定义 @RequestMapping 方法来显式允许匿名(未经授权)访问? 以下方法不起作用,总是收到 401 Unauthorized: @RequestMapping("/test") @Se
想象一下我有一个像这样的 Spring MVC Controller : @Controller @RequestMapping("/base-url") public class MyControl
如何处理除资源映射文件夹之外的所有 URL 请求? 在这个例子中,我处理了所有路径(这就是我想要的),但现在即使我设置也无法访问资源文件夹 里面dispatcher-servlet.xml @Req
我需要有一个/jsp/login 和/jsp/login?error 的映射 请注意,我无法使用/jsp/login?error=someval 然后检查参数是否具有该值或默认值。在这种情况下,不允许
让 Tomcat 启动并自动显示 index.jsp 文件的内容没有任何问题。然而,当我尝试使用 @Controller 和 @RequestMapping 时,我遇到了错误。这是我的 Control
我有以下文件夹结构: WEB-INF/ -- 页数/ ----home.jsp ----admin.jsp ----admin2.jsp ----admin3.jsp 我有以下 Controller
我正在使用 Spring Boot,并尝试显示 get 请求。我正在使用 2 个类:Greeting.java 和 GreetingController: package greeting;
我正在尝试使用 @RequestMapping与 consumes -元素。阅读API-document它适用于 Content-Type - 请求头。但是,使用 @RequestMapping(co
我已经为此打开了一个问题 here.但我也想问一下 stackoverflow 的人。 @Controller @RequestMapping("/xxx") public class MyContr
我想在我的 Spring Boot 应用程序中创建一个自定义注释,它总是向我的类级别 RequestMapping 路径 添加一个前缀。 我的 Controller : import com.sage
鉴于下面的@Controller,即使我向 webApp 发送 Get 请求, Controller 也会运行 homePage 方法。 @RestController @RequestMapping
我正在尝试设置 Controller ,但不幸的是无法查看输出...一切都正确渲染。当我访问 http://localhost:8080/CMT/content/edit 时,我得到一个 404 页面
在我的 Spring Controller 中,我创建了 3 个方法。方法 1 和方法 2 工作正常,但方法 3 给我带来问题 问题: org.springframework.web.servlet.
我是一名优秀的程序员,十分优秀!