@RequestMapping注解返回错误的URL

时间:2019-02-01 17:20:41

标签: spring-mvc request-mapping

当我运行TomCat时,映射似乎正确完成了。

控制台的信息:

信息:映射 “{[/ cervejas /从头]}” 到公共java.lang.String中com.algaworks.brewer.controller.CervejasController.novo()

然而,当我尝试做调用以下网址:本地主机:8080 /啤酒/ cervejas /从头错误HTTP状态404返回

我得到在控制台上的Tomcat如下警告,当我试图访问浏览器映射网址:

警告:没有映射找到HTTP请求与URI [/啤酒/ cervejas / cerveja / CadastroCerveja]在DispatcherServlet的名为 '调度'

还有一个 “目录”(cervejas)的URI。

WebConfig.java文件:

package com.algaworks.brewer.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ITemplateResolver;

import com.algaworks.brewer.controller.CervejasController;

@Configuration
@ComponentScan(basePackageClasses = { CervejasController.class } )
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public ViewResolver viewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }

    @Bean
    public TemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setEnableSpringELCompiler(true);
        engine.setTemplateResolver(templateResolver());
        return engine;
    }

    private ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(applicationContext);
        resolver.setPrefix("classpath:/templates/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode(TemplateMode.HTML);
        return resolver;
    }

}

CervejasController.java文件:

package com.algaworks.brewer.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class CervejasController {

    @RequestMapping("/cervejas/novo")
    public String novo() {
        return "cerveja/CadastroCerveja";
    }

}

CadastroCerveja.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<body>
    <h1>Olá ! Thymeleaf configurado.</h1>
</body>

</html>

WebConfig.java文件:

package com.algaworks.brewer.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ITemplateResolver;

import com.algaworks.brewer.controller.CervejasController;

@Configuration
@ComponentScan(basePackageClasses = { CervejasController.class } )
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public ViewResolver viewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }

    @Bean
    public TemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setEnableSpringELCompiler(true);
        engine.setTemplateResolver(templateResolver());
        return engine;
    }

    private ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(applicationContext);
        resolver.setPrefix("classpath:/templates/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode(TemplateMode.HTML);
        return resolver;
    }

}

Package Explorer:

enter image description here

我无法找到该错误。感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您正在尝试访问未在控制器中定义的URL: / brewer / cervejas / novo

如果未在application.properties或某个Configuration Class中为应用程序配置 ContextPath ,则应通过选择以下选项之一:

  • 通过放置application.properties属性在server.servlet.context-path=/brewer文件中定义上下文路径
  • 使您的Controller基本映射点指向/brewer,如下所示:

    package com.algaworks.brewer.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @RequestMapping("/brewer")
    public class CervejasController {
    
        @RequestMapping("/cervejas/novo")
        public String novo() {
            return "cerveja/CadastroCerveja";
        }
    

    }

答案 1 :(得分:0)

请尝试将您的请求类型(GET,POST等)添加到控制器方法中,如下所示:

注意:我不知道您要尝试访问URI的请求方法,但我假设它是GET请求。您应该根据需要更改请求类型。

package com.algaworks.brewer.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/brewer")
public class CervejasController {

    @RequestMapping("/cervejas/novo", method = RequestMethod.GET)
    public String novo() {
        return "cerveja/CadastroCerveja";
    }
}