[英]Creating Gradle file for REST-Web Service
I am pretty new to gradle and want to create a buildfile for a REST-Service. 我是新手,想要为REST服务创建构建文件。 After I read some introduction on the gradle homepage and some other pages I started a new project using the command gradle init --type=java-project
because my service is Java based. 在阅读gradle主页和其他页面上的一些介绍之后,我使用gradle init --type=java-project
命令开始了一个新项目,因为我的服务是基于Java的。 The structure of the project was created and I wanted to insert my .class
files into the directories. 项目的结构已创建,我想将.class
文件插入目录中。 This is my first problem: I don´t know the proper structure for this kind of project. 这是我的第一个问题:我不知道这种项目的正确结构。 After this I focused on my dependencies, which I added. 在此之后,我重点介绍了我的依赖项(已添加)。 As soon as I want to build my project, an error occurs which has to do with the usage of jersey as technology. 一旦我要构建项目,就会发生错误,这与将jersey作为技术使用有关。 If I compile my service with eclipse, it works perfectly fine, so my code works. 如果我用eclipse编译我的服务,它可以很好地工作,所以我的代码也可以工作。
So my 2 questions are: 所以我的两个问题是:
How to create an proper file structure ? 如何创建合适的文件结构?
How to fix the issue with the Servlet/WAR-compilation 如何使用Servlet / WAR编译解决问题
Here is the structure of my project in eclipse: 这是我在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
I have no plan where to place the two files from the WEB-INF directory. 我没有计划将WEB-INF目录中的两个文件放在何处。 I have placed the structure under the projectfolder /src/main/java/com/name/restproject
我已将结构放在项目文件夹/src/main/java/com/name/restproject
Here is the gradle file of the project 这是项目的gradle文件
build.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'
}
If you want to create a REST web service with a Gradle build, I suggest you to use Spring Boot REST 如果您想使用Gradle构建来创建REST Web服务,建议您使用Spring Boot REST
Spring Boot - Building a RESTful Web Service : http://spring.io/guides/gs/rest-service/ Spring Boot-构建RESTful Web服务: http : //spring.io/guides/gs/rest-service/
It already contains a Gradle build : https://github.com/spring-guides/gs-rest-service/blob/master/initial/build.gradle 它已经包含Gradle构建: https : //github.com/spring-guides/gs-rest-service/blob/master/initial/build.gradle
Also if you want to build a WAR instead of a JAR (which is default build configuration), you can follow this documentation : http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-gradle-packaging 另外,如果要构建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.