春季-web.xml不正确包含url模式声明

时间:2018-11-22 15:45:30

标签: java spring spring-mvc

我正在尝试在Spring MVC上发出GET请求,它向我回复404和警告

WARN (org.springframework.web.servlet.PageNotFound) - No mapping found for HTTP request with URI [/bo/newsletter/api/104] in DispatcherServlet with name 'dispatcher'

我将90%地确定问题出在我的web.xml中,正如我将在稍后解释的那样。

这是我的RestController:

@RequestMapping("newsletter/api")
@RestController
public class NewsletterRestController {

    @Autowired
    INewsletterService newsletterService;

    @Autowired
    public NewsletterRestController(INewsletterService newsletterService) {
        this.newsletterService = newsletterService;
    }

    @InitBinder
    public void initBinder(final WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }

    @PutMapping
    public ResponseEntity sendNews(
            @RequestBody Newsletter news,
            final User user) throws IOException {
        news.setLastUserId(user.getId());
        newsletterService.saveNewsletter(user, news);
        return new ResponseEntity(HttpStatus.CREATED);
    }

    @GetMapping("/{id}")
    public Newsletter getNewsletter(@PathVariable Long id) {
        return newsletterService.getOneNewsletter(id);
    }

}

这是我的web.xml(更有趣的部分):

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
                classpath:spring/applicationContext-model.xml,

                classpath:spring/applicationContext-mail.xml,

                classpath:spring/applicationContext-dbcp.xml,
                classpath:spring/applicationContext-dao.xml,
                classpath:spring/applicationContext-service.xml,

                classpath:spring/applicationContext-taxref-dbcp.xml,
                classpath:spring/applicationContext-taxref-dao.xml,
                classpath:spring/applicationContext-taxref-service.xml,

                classpath:spring/applicationContext-security-bo.xml,                               
                classpath:spring/dispatcher-servlet.xml                
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:spring/dispatcher-servlet.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/newsletter/api/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/newsletter/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/index/*</url-pattern>
    </servlet-mapping>

有趣的是,如果我将/newsletter/api/* <url-pattern>修改为/newsletter/api/104(例如,如果我尝试获取ID 104),则GET请求可以魅力。

问题在于,我认为在这种情况下,/newsletter/*作为url模式,可以合理地覆盖/ newsletter url的每个扩展。

有什么建议吗?

先谢谢了

- 丹尼斯

0 个答案:

没有答案