我得到了一个意外错误(type = Not Found,status = 404)错误?

时间:2016-09-25 16:19:23

标签: java spring hibernate spring-mvc spring-boot

我收到错误在构建成功时,在mybrowser中运行时出现意外错误(type = Not Found,status = 404)错误。
为什么我的应用程序找不到我的jsp页面?为了找到这些,我应该包括哪些内容?

application_configuration

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.EventToday.event")
public class ApplicationConfig {

           @Bean
            public ViewResolver viewResolver() {
                InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
                viewResolver.setViewClass(JstlView.class);
                viewResolver.setPrefix("/WEB-INF/pages/");
                viewResolver.setSuffix(".jsp");
                return viewResolver;
            }

            @Bean
            public MessageSource messageSource() {
                ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
                messageSource.setBasename("messages");
                return messageSource;
            }

}

#hibernate_configuration
@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.EventToday.event" })
@PropertySource(value = { "classpath:application.properties" })
public class hibernateconfig {

        @Autowired
        private Environment environment;

        @Bean
        public LocalSessionFactoryBean sessionFactory() {
            LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
            sessionFactory.setDataSource(dataSource());
            sessionFactory.setPackagesToScan(new String[] { "com.EventToday.event.model" });
            sessionFactory.setHibernateProperties(hibernateProperties());
            return sessionFactory;
         }

        @Bean
        public DataSource dataSource() {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName(environment.getRequiredProperty("spring.datasource.driverClassName"));
            dataSource.setUrl(environment.getRequiredProperty("spring.datasource.url"));
            dataSource.setUsername(environment.getRequiredProperty("spring.datasource.username"));
            dataSource.setPassword(environment.getRequiredProperty("spring.datasource.password"));
            return dataSource;
        }

        private Properties hibernateProperties() {
            Properties properties = new Properties();
            properties.put("spring.jpa.properties.hibernate.dialect", environment.getRequiredProperty("spring.jpa.properties.hibernate.dialect"));
            properties.put("spring.jpa.show_sql", environment.getRequiredProperty("spring.jpa.show-sql"));
            properties.put("spring.jpa.properties.hibernate.format_sql", environment.getRequiredProperty("spring.jpa.properties.hibernate.format_sql"));
            return properties;        
        }

        @Bean
        @Autowired
        public HibernateTransactionManager transactionManager(SessionFactory s) {
           HibernateTransactionManager txManager = new HibernateTransactionManager();
           txManager.setSessionFactory(s);
           return txManager;
        }


}


#service_controller

@RestController
@RequestMapping("/")
public class eventController {

    @Autowired
    private eventsServices eventsservices;


    @RequestMapping(value="/events",
            method=RequestMethod.GET
            //produces=MediaType.APPLICATION_JSON_VALUE
            )
    public ResponseEntity<Collection<events>> getEvents(){
        Collection<events> evnts = eventsservices.findAll();

        return new ResponseEntity<Collection<events>>(evnts,HttpStatus.OK);

    }

    @RequestMapping(value="/events/{id}",
            method=RequestMethod.GET
            //produces=MediaType.APPLICATION_JSON_VALUE
            )
    public ResponseEntity<events> getEvent(@PathVariable("id") int id){
        events evnts = eventsservices.findById(id);
        if(evnts==null){
            return new ResponseEntity<events>(evnts, HttpStatus.NOT_FOUND);
        }

        return new ResponseEntity<events>(evnts,HttpStatus.OK);

    }
}

#pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.EventToday</groupId>
    <artifactId>EventToday</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>EventToday</name>
    <description>projects for events</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <springframework.version>4.0.6.RELEASE</springframework.version>
        <hibernate.version>4.3.6.Final</hibernate.version>
        <mysql.version>5.1.31</mysql.version>
        <joda-time.version>2.3</joda-time.version>
        <testng.version>6.9.4</testng.version>
        <mockito.version>1.10.19</mockito.version>
        <h2.version>1.4.187</h2.version>
        <dbunit.version>2.2</dbunit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency> 
        <!-- <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency> -->
        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            </dependency>

        <!-- jsr303 validation -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>

        <!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>1.5.2</version>
        </dependency>

        <!-- < Servlet+JSP+JSTL >
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

#/WEB-INF/pages/ in in src/java/webapp/

what i'm missing here?

2 个答案:

答案 0 :(得分:2)

您需要将 @ResponseBody 放在其余方法上。

此标记告诉Spring返回对象是响应的内容。

答案 1 :(得分:0)

正如Leandro Dantas所说,你应该在你的休息方法上添加 @ResponseBody 。即

@RequestMapping(value="/events",
        method=RequestMethod.GET
        //produces=MediaType.APPLICATION_JSON_VALUE
        )
@ResponseBody
public ResponseEntity<Collection<events>> getEvents(){
    Collection<events> evnts = eventsservices.findAll();

    return new ResponseEntity<Collection<events>>(evnts,HttpStatus.OK);

}

指示方法返回值的注释应绑定到Web响应正文link