spring - Gradle +嵌入式 jetty

标签 spring hibernate gradle jetty

我已经修改了一个现有的Jetty项目,并且在构建之后获得了404。也许我需要修改其他我不知道的文件。
我正在使用gradle构建。这是build.gradle:

apply plugin: 'java'
apply plugin: 'jetty'
apply plugin: 'eclipse'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.springframework:spring-webmvc:4.1.6.RELEASE'
    compile  'org.hibernate:hibernate-core:4.3.6.Final'
    testImplementation 'junit:junit:4.12'
    testCompile 'junit:junit:4.11'
    testCompile 'org.hamcrest:hamcrest-all:1.3'
    compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.14'
}
test {
    exclude '**/*IntegrationTest*'
}

task integrationTest(type: Test) {
    include '**/*IntegrationTest*'
    doFirst {
        jettyRun.contextPath = '/';
        jettyRun.httpPort = 8080    // Port for test
        jettyRun.daemon = true
        jettyRun.execute()
    }
    doLast {
        jettyStop.stopPort = 8091   // Port for stop signal
        jettyStop.stopKey = 'stopKey'
        jettyStop.execute()
    }
}


// Embeded Jetty for testing
jettyRun{
    contextPath = "spring4"
    httpPort = 8080
}

jettyRunWar{
    contextPath = "spring4"
    httpPort = 8080
}


//For Eclipse IDE only
eclipse {

  wtp {
    component {

      //define context path, default to project folder name
      contextPath = 'spring4'

    }

  }
}

这里是另一类:
package com.hello.webapp;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import com.hello.service.SignUpService;

@Path("/signUp")
public class SignUpWebapp {
    private static SignUpService signUpService = new SignUpService();

    @GET()
    public String hello() {
        return signUpService.sayHello();
    }
}

这里的简单服务:
package com.hello.service;

public class SignUpService {

    public String sayHello() {
        return "signUp";
    }

}

这是另一个集成测试类
package com.hello.webapp;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;

import org.junit.Test;

public class SignUpIntegrationTest {
    private static String SIGNUP = "http://localhost:8080/signUp";

    @Test
    public void testHello() throws Exception {
        Client client = ClientBuilder.newClient();
        WebTarget webTarget = client.target(SIGNUP);
        String response = webTarget.request().get(String.class);

        assertThat(response, is("signUp"));
    }
}

因此,当我运行gradle integrationTest时,我收到一条错误消息,提示Jetty已经在运行。当我尝试访问localhost/signUp时,我得到了404。

最佳答案

一种解决方案是改用gretty:

apply plugin: 'org.akhikhl.gretty'
....
classpath 'org.akhikhl.gretty:gretty:1.4.0'
...
gretty {
    port = 8083
    contextPath = '/'
    servletContainer = 'jetty9'
    jvmArgs = [
            '-Xms700m',
            '-Xmx2048m'
            ]
    loggingLevel='INFO'
    debugPort = 5005      // default
    debugSuspend = true   // default

    httpsEnabled = true
    httpsPort = 8443
    sslKeyStorePath = 'some.jks'
    sslKeyStorePassword = 'somepwd'
}

然后gradle appStart

让我知道是否有帮助。

关于spring - Gradle +嵌入式 jetty ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42741226/

相关文章:

java - 在 REST 服务应用程序中实现 Correlation Id

java - hibernate/ Spring : Lots (thousands) of open connections the database

java - 在 UI 中使用之前加载 Hibernate 对象图的最佳方法是什么?

android - 带有 Android Gradle 的 Jenkins 无法解析 com.android.tools.build :gradle:1. 3.1

java - ClassPathXmlApplicationContext 错误,Spring 框架

java - 添加另一个 Spring MVC 项目作为依赖项后 Spring Boot 找不到合适的 URL 映射

java - Hibernate 当系统时间等于字段中的值时删除条目/行

java - 对带有删除标志的数据使用 HQL 的正确方法是什么

android - flutter 1.9.1+hotfix.2 - 构建 appbundle 失败,但实际上生成了一个 appbundle

gradle - 运行 Gradle 的 PMD 插件时在类路径中包含 jar