簡體   English   中英

彈簧 RequestMappingHandlerMapping 不起作用

[英]spring RequestMappingHandlerMapping not working

我從 Spring Boot 開始,因為我正在學習教程。 在本教程中,他們使用 @RequestMapping 和 GET 方法創建了控制器,一旦他們運行了應用程序,控制台中就會顯示如下內容:

swsmmaRequestMappingHandlerMapping :將“{[/rooms], methods = GET}”映射到 java.util.List<..//more 行

但就我而言,我遇到了一個錯誤:

swsmmaRequestMappingHandlerMapping :將“{[/error]}”映射到公共 org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)

為什么不創建映射?

這是控制器:

package com.frankmoley.london.data.webservice;

import com.frankmoley.london.data.entity.Room;
import com.frankmoley.london.data.repository.RoomRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;

@RestController
public class RoomController {

    @Autowired
    private RoomRepository repository;

    @RequestMapping(value="/rooms", method= RequestMethod.GET)
    List<Room> findAll(@RequestParam(required=false) String roomNumber){

       List<Room> rooms = new ArrayList<>();

        if(null==roomNumber){

            Iterable<Room> results = this.repository.findAll();
            results.forEach(room-> {rooms.add(room);});
        }else{

            Room room = this.repository.findByNumber(roomNumber);
            if(null!=room) {
                rooms.add(room);
            }
        }
        return rooms;
    }
}

實體:

package com.frankmoley.london.data.entity;

import javax.persistence.*;

@Entity
@Table(name = "ROOM")
public class Room {

    @Id
    @Column(name = "ROOM_ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    @Column(name = "NAME")
    private String name;
    @Column(name = "ROOM_NUMBER")
    private String number;
    @Column(name = "BED_INFO")
    private String info;

//getters and setters
}

存儲庫:

package com.frankmoley.london.data.repository;

import com.frankmoley.london.data.entity.Room;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface RoomRepository extends CrudRepository<Room, Long> {
    Room findByNumber(String number);

}

以下是我的日志:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.8.RELEASE)

2019-09-09 21:41:37.074  INFO 25569 --- [           main] SpringRest.spring_rest.Application       : Starting Application on prashant-ubuntu with PID 25569 (/home/prashant/workspace/egen/spring-rest/target/spring-rest-1.0.0.jar started by prashant in /home/prashant/workspace/egen/spring-rest/target)
2019-09-09 21:41:37.077  INFO 25569 --- [           main] SpringRest.spring_rest.Application       : No active profile set, falling back to default profiles: default
2019-09-09 21:41:37.715  INFO 25569 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-09-09 21:41:37.796  INFO 25569 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 68ms. Found 1 repository interfaces.
2019-09-09 21:41:38.389  INFO 25569 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$d6eeeeff] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-09-09 21:41:38.696  INFO 25569 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-09-09 21:41:38.735  INFO 25569 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-09-09 21:41:38.735  INFO 25569 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-09-09 21:41:38.837  INFO 25569 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-09-09 21:41:38.837  INFO 25569 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1701 ms
2019-09-09 21:41:39.124  INFO 25569 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-09-09 21:41:39.415  INFO 25569 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2019-09-09 21:41:39.463  INFO 25569 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2019-09-09 21:41:39.525  INFO 25569 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.3.11.Final}
2019-09-09 21:41:39.526  INFO 25569 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2019-09-09 21:41:39.662  INFO 25569 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-09-09 21:41:39.866  INFO 25569 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2019-09-09 21:41:40.463  INFO 25569 --- [           main] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory
2019-09-09 21:41:40.559  INFO 25569 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-09-09 21:41:41.041  INFO 25569 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-09 21:41:41.114  WARN 25569 --- [           main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2019-09-09 21:41:41.380  INFO 25569 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-09-09 21:41:41.382  INFO 25569 --- [           main] SpringRest.spring_rest.Application       : Started Application in 4.703 seconds (JVM running for 5.103)
2019-09-09 21:42:03.922  INFO 25569 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-09-09 21:42:03.922  INFO 25569 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2019-09-09 21:42:03.934  INFO 25569 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 12 ms
Hibernate: select employee0_.id as id1_0_, employee0_.email as email2_0_, employee0_.name as name3_0_, employee0_.salary as salary4_0_ from employee employee0_

你可以清楚地看到 RequestMappingHandlerMapping 沒有任何引用

但該服務仍托管在“/”。

我使用了以下網址:

http://localhost:8080/employees/springEntityManagerJpa

其他非spring boot的spring應用,URL如下:

http://localhost:8080/spring-rest/api/employees/springEntityManagerJpa

我假設你是類似的情況。 可能是服務已啟動,但您提供了錯誤的端點。 只需刪除上下文路徑。 就我而言,我刪除了/spring-rest/api

希望這可以幫助!!!

對於子孫后代。 轉到Exercise文件並導入現有項目

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM