是否可以在方法中使用多个@RequestMapping spring注释?喜欢:
@RequestMapping("/")
@RequestMapping("")
@RequestMapping("/welcome")
public String welcomeHandler(){
return("welcome");
}
Run Code Online (Sandbox Code Playgroud) Is there a way to get the complete path value after the requestMapping @PathVariable values have been parsed?
这就是:
/{id}/{restOfTheUrl}应该可以解析/1/dir1/dir2/file.html成id=1和restOfTheUrl=/dir1/dir2/file.html
任何想法,将不胜感激.
我想自己处理请求和会话属性,而不是将其留给spring @SessionAttributes,例如登录cookie处理.
我只是无法弄清楚如何HttpRequest在控制器中访问from,我需要一种方法去上面一层@RequestAttribute并访问它HttpRequest自己.使用Stripes来实现ApplicationContext和调用getAttribute().
此外,传递HttpServletRequestas参数似乎不起作用:
@RequestMapping(value="/") public String home(HttpServletRequest request){
System.out.println(""+request.getSession().getCreationTime());
return "home";
}
Run Code Online (Sandbox Code Playgroud)
上述方法不打印任何内容.
你对此有什么建议吗?
我有一个支持这两个GET和POST请求的资源.这是示例资源的示例代码:
@RequestMapping(value = "/books", method = RequestMethod.GET)
public ModelAndView listBooks(@ModelAttribute("booksFilter") BooksFilter filter, two @RequestParam parameters, HttpServletRequest request)
throws ParseException {
LONG CODE
}
@RequestMapping(value = "/books", method = RequestMethod.POST)
public ModelAndView listBooksPOST(@ModelAttribute("booksFilter") BooksFilter filter, BindingResult result)
throws ParseException {
SAME LONG CODE with a minor difference
}
Run Code Online (Sandbox Code Playgroud)
两个方法中的代码实际上是相同的,除了让我们说变量定义.这两种方法可以很容易地组合使用method = {RequestMethod.POST, RequestMethod.GET},并且if内部简单.我尝试,但它不工作的,因为这两种方法具有不同的参数在末端,即HttpServletRequest和BindingResult(所述@RequestParam的不要求,因此在不需要的POST请求).任何想法如何结合这两种方法?
我有一个简单的控制器,如下所示: -
@Controller
@RequestMapping(value = "/groups")
public class GroupsController {
// mapping #1
@RequestMapping(method = RequestMethod.GET)
public String main(@ModelAttribute GroupForm groupForm, Model model) {
...
}
// mapping #2
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String changeGroup(@PathVariable Long id, @ModelAttribute GroupForm groupForm, Model model) {
...
}
// mapping #3
@RequestMapping(method = RequestMethod.POST)
public String save(@Valid @ModelAttribute GroupForm groupForm, BindingResult bindingResult, Model model) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,此页面具有以下功能: -
/groups GET)./groups POST)或选择特定组(/groups/1 GET …我正在尝试构建一个应用程序,它可以列出数据库中的一些值,并在必要时使用Spring 4修改,添加,删除,并且我收到以下错误(仅当我的两个控制器文件中都存在"@Controller"注释时,如果我从其中一个文件中删除注释,但我在控制台中收到一条消息"没有找到映射...在带有名称的dispatcherservlet中......":
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/edit/{id}],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.bookReview.app.BookController.editBook(int,org.springframework.ui.Model)
WARN : org.springframework.web.context.support.XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'reviewController' bean method
public java.lang.String com.bookReview.app.ReviewController.editReview(int,org.springframework.ui.Model)
to {[/edit/{id}],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'bookController' bean method
public java.lang.String com.bookReview.app.BookController.editBook(int,org.springframework.ui.Model) mapped.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) …Run Code Online (Sandbox Code Playgroud) 我有以下方法:
@RequestMapping(value = "/path/to/{iconId}", params="size={iconSize}", method = RequestMethod.GET)
public void webletIconData(@PathVariable String iconId, @PathVariable String iconSize, HttpServletResponse response) throws IOException {
// Implementation here
}
Run Code Online (Sandbox Code Playgroud)
我知道如何使用@PathVariable从RequestMapping传递变量"webletId",但是如何从params引用变量"iconSize"?
非常感谢.
我想编写自定义注释,根据注释修改Spring请求或路径参数.例如,而不是这段代码:
@RequestMapping(method = RequestMethod.GET)
public String test(@RequestParam("title") String text) {
text = text.toUpperCase();
System.out.println(text);
return "form";
}
Run Code Online (Sandbox Code Playgroud)
我可以制作注释@UpperCase:
@RequestMapping(method = RequestMethod.GET)
public String test(@RequestParam("title") @UpperCase String text) {
System.out.println(text);
return "form";
}
Run Code Online (Sandbox Code Playgroud)
它是否可能,如果是,我怎么能这样做?
这可能吗?
@Controller
@RequestMapping("/login")
public class LoginController {
@RequestMapping("/")
public String loginRoot() {
return "login";
}
@RequestMapping(value="/error", method=RequestMethod.GET)
public String loginError() {
return "login-error";
}
}
Run Code Online (Sandbox Code Playgroud)
我在访问时遇到404错误,localhost:8080/projectname/login但没有localhost:8080/projectname/login/error.
这是我的web.xml项目名称
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<description></description>
<servlet-name>projectname</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>projectname</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud) 我遇到了麻烦!正如我一直在阅读的那样,这种情况的发生是因为它不再被接受。但我该如何解决呢?这是我一直在尝试映射的代码。
@FeignClient(name = "product-service")
@RequestMapping("api/products/")
public interface ProductClient {
@GetMapping("/{id}")
ResponseEntity<Product> getProduct(@PathVariable("id") Long id);
@GetMapping("/{id}/stock")
ResponseEntity<Product> updateStockProduct(@PathVariable("id") Long id,@RequestParam(name = "quantity", required = true) Integer quantity);
}
Run Code Online (Sandbox Code Playgroud)
预先感谢您的任何建议或解决方案!
request-mapping ×10
spring ×8
spring-mvc ×8
java ×6
http-verbs ×1
httprequest ×1
httpsession ×1
maven ×1
mv ×1
servlets ×1
spring-boot ×1
url-routing ×1