簡體   English   中英

為REST-Web服務創建Gradle文件

[英]Creating Gradle file for REST-Web Service

我是新手,想要為REST服務創建構建文件。 在閱讀gradle主頁和其他頁面上的一些介紹之后,我使用gradle init --type=java-project命令開始了一個新項目,因為我的服務是基於Java的。 項目的結構已創建,我想將.class文件插入目錄中。 這是我的第一個問題:我不知道這種項目的正確結構。 在此之后,我重點介紹了我的依賴項(已添加)。 一旦我要構建項目,就會發生錯誤,這與將jersey作為技術使用有關。 如果我用eclipse編譯我的服務,它可以很好地工作,所以我的代碼也可以工作。

所以我的兩個問題是:

如何創建合適的文件結構?

如何使用Servlet / WAR編譯解決問題


這是我在Eclipse中的項目結構:

RestWebService
|--Java Resources
|  |--src
|     |--com.name.restproject.rest
|     |  |--WebController.java         //contains the jersey REST service
|     |--com.name.restproject.service
|        |--ServiceUtil.java           //contains some util methods
|--Libraries
|  |--Apache Tomcat v7
|  |--asm-3.3.1.jar
|  |--EAR Libraries
|  |--gson-2.5.jar
|  |--jersey-bundle-1.14.jar
|  |--JRE SYSTEM Library
|  |--json-20151123.jar
|  |--Web App Libraries
|--JavaScript Resources
|--build
|--WebContent
   |--META-INF
   |  |--MANIFEST.MF
   |--WEB-INF
      |--lib
      |--fileName.properties           //File with Data for ServiceUtil.java
      |--configFile.xml                //File with Confc for ServiceUtil.java
      |--web.xml                       //Config for the REST paths

我沒有計划將WEB-INF目錄中的兩個文件放在何處。 我已將結構放在項目文件夾/src/main/java/com/name/restproject

這是項目的gradle文件

build.gradle

apply plugin: 'java'
apply plugin: 'war'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    mavenCentral()
}

// In this section you declare the dependencies for your production and test code
dependencies {
    // The production code uses the SLF4J logging API at compile time
    compile group: 'com.google.code.gson', name: 'gson-parent', version: '2.6'
    compile group: 'asm', name: 'asm-parent', version: '3.3.1'
    compile group: 'com.sun.jersey.glassfish.v3.osgi', name: 'jersey-gf-bundle', version: '1.1.4'
    compile group: 'org.json', name: 'json', version: '20151123'


    // Declare the dependency for your favourite test framework you want to use in your tests.
    // TestNG is also supported by the Gradle Test task. Just change the
    // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
    // 'test.useTestNG()' to your build script.
    testCompile 'junit:junit:4.12'
}

如果您想使用Gradle構建來創建REST Web服務,建議您使用Spring Boot REST

Spring Boot-構建RESTful Web服務: http : //spring.io/guides/gs/rest-service/

它已經包含Gradle構建: https : //github.com/spring-guides/gs-rest-service/blob/master/initial/build.gradle

另外,如果要構建WAR而不是JAR(這是默認的構建配置),則可以遵循以下文檔: http : //docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build -tool-plugins-gradle-packaging

暫無
暫無

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

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