使用gradle设置休息服务

时间:2018-05-27 19:43:16

标签: java rest gradle

我第一次尝试使用gradle但是在创建一个简单的项目框架时遇到了困难,我可以从中进行处理。

Project Structure

这是我的build.gradle内容

    buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.2.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'

bootJar {
    baseName = 'backend'
    version '1.0-SNAPSHOT'
}
war{
    baseName = 'api'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

使用以下命令

可以很好地构建并运行良好
gradle clean build

gradle bootRun

但是,我没有看到任何可以使用的上下文路径

2018-05-27 20:32:27.107  INFO 8049 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-05-27 20:32:27.108  INFO 8049 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-05-27 20:32:27.129  INFO 8049 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-27 20:32:27.129  INFO 8049 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-27 20:32:27.231  INFO 8049 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-05-27 20:32:27.271  INFO 8049 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-05-27 20:32:27.275  INFO 8049 --- [           main] com.initiator.Application                : Started Application in 1.976 seconds (JVM running for 2.342)
2018-05-27 20:32:41.119  INFO 8049 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-05-27 20:32:41.119  INFO 8049 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-05-27 20:32:41.133  INFO 8049 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 14 ms

我尝试了以下网址

http://localhost:8080/
http://localhost:8080/api
http://localhost:8080/api/greeting

他们都没有工作,我得到了

    Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

知道我在这里做错了吗?

1 个答案:

答案 0 :(得分:1)

Spring Boot默认会扫描Application类及其子包的包以进行注释。在您的代码结构概述中,您的应用程序似乎位于com.initiator,而您的实体和控制器位于com.controllercom.entity。当您使用@SpringBootApplication注释主类时,不会使用默认行为扫描这两个包中的实体或控制器。

您有两种可能性:

  1. 制作Application类的实体和控制器包子包。
  2. 指定scanBasePackages属性以包含控制器和实体包,这些包不是Application类的子包,例如@SpringBootApplication(scanBasePackages = { "com.entity", "com.controller" })
  3. 我建议坚持使用第一个选项并坚持使用默认行为(减少工作量,明确其他人搜索带注释类的位置)。