【问题标题】:Spring boot endpoint pathSpring Boot 端点路径
【发布时间】:2020-01-18 01:55:51
【问题描述】:

我的 IntelliJ 项目有一些问题。我正在尝试使用 Postman 达到终点,但我什至不知道这样做的路径是什么!我尝试了所有可能的路径,但没有任何效果!刚拿到404状态。 这是我的restController:

@RestController
@RequestMapping("/cities")
public class CityService extends GenericService<City> {

    @Autowired
    private CityRepository cityRepository;

    /**
     * Method that returns all the cities.
     * @return {@link List<City>}
     */
    @Override
    @RequestMapping(method = RequestMethod.GET)
    public List<City> loadAll(){
        System.out.println(new Date() + " Called LOAD-ALL.");
        return cityRepository.findAll();
    }
}

我的通用服务:

public class GenericService<T> {

    @Autowired
    GenericRepository<T> genericRepository;

    public GenericRepository<T> getGenericRepository(){
        return genericRepository;
    }

    /**
     * Method that returns all the generic entities.
     * @return {@link List<T>}
     */
    @GetMapping
    @RequestMapping(method = RequestMethod.GET)
    public List<T> loadAll(){
        System.out.println(new Date() + " Called LOAD-ALL.");
        return getGenericRepository().findAll();
    }

我的 config.properties:

spring.application.name = authorization-service
server.servlet.context-path = /authorization/api
#server.context-path = /authorization/api
#server.contextPath = /authorization/api
#server.module-path=/

spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.ddl = true
spring.jpa.show-sql = true

hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

server.port = 8787

eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://localhost:8761/eureka}

eureka.instance.instance.instance-id = ${spring.application.name}:${server.port}:${random.int}

spring.cloud.loadbalancer.ribbon.enabled = false

spring.datasource.url = jdbc:mysql://localhost:3306/my_database? 
createDatabaseIfNotExist=true
spring.datasource.username = someUser
spring.datasource.password = somePass

我试图在 Postman http://localhost:8787/authorization/api/cities 上使用此链接,但没有任何效果。 有人可以帮助我吗?我对映射端点这件事有点陌生。 我正在使用 Spring Boot 2.2.2.RELEASE。 启动消息是

2020-01-18 09:06:10.023  INFO 2808 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8787 (http) with context path '/authorization/api'

【问题讨论】:

    标签: java spring-boot path mapping endpoint


    【解决方案1】:

    请尝试使用该属性。

    server.servlet.context-path = /authorization/api
    

    另外,当 spring boot 应用程序启动时,上下文路径将显示在日志中,如下所示

    [2m2020-01-18 08:41:04.513[0;39m [32m INFO[0;39m [35m13986[0;39m [2m---[0;39m [2m[主][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat 在端口上启动:8080 (http) 和上下文路径 '/authorization/api'

    还有以下

    @GetMapping
    @RequestMapping(method = RequestMethod.GET, value = "")
    

    意思相同。请查看@GetMapping

    更新 Op 存在组件扫描问题,并且未自动检测到控制器类。

    【讨论】:

    • 试过这个但遇到了同样的问题。我还删除了“@GetMapping”和“value = ''”,因为听起来多余但仍然得到 404。当我拨打localhost:8787/authorization/api/cities 之类的电话时,他点击了我的后端,但说他无法解析 HTTP请求路径上的字符或其他内容。
    • 应用程序启动时记录的上下文路径是什么?请使用错误和您尝试使用的最新代码以及 Spring Boot 版本更新问题
    • 2020-01-18 09:06:10.023 INFO 2808 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer:Tomcat 在端口上启动:8787 (http),上下文路径为 '/ authentication/api' 这是登录到我的路径!
    • 当您使用浏览器点击此网址时会发生什么?另外,你可以尝试修改方法如下@Override @RequestMapping(method = RequestMethod.GET) public String loadAll(){ return "test"; }
    • 我已经完全按照您的建议进行了尝试,并且发生了同样的情况。浏览器响应:Whitelabel 错误页面 此应用程序没有显式映射 /error,因此您将其视为后备。 2020 年 1 月 18 日星期六 09:57:36 BRT 出现意外错误(类型=未找到,状态=404)。没有可用的消息
    猜你喜欢
    • 1970-01-01
    • 2021-08-10
    • 2020-06-01
    • 2016-03-13
    • 2019-03-24
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多