Spring Cloud Contract with Jersey

爷,独闯天下 提交于 2021-01-29 03:30:02

问题


I have a simple project Spring boot project. It contains one Jersey based Controller: @Path("persons") @Produces(MediaType.APPLICATION_JSON) public class PersonsController {

    @GET
    public Person get() {
        return new Person("James", 20);
    }
}

It returns json response as expected (url: http://localhost:PORT/persons):

{
  "name": "James",
  "age": 20
}

My aim is to add Spring Cloud Contract tests for this controller. I have added all required mvn configurations, and test:

public class MvcTest {
    @Before
    public void setup() {
        RestAssuredMockMvc.standaloneSetup(new PersonsController());
    }
}

Here is contract (groovy file): import org.springframework.cloud.contract.spec.Contract

Contract.make {
    request {
        method 'GET'
        url('persons')
    }
    response {
        status 200
        body(
                "name": "James",
                "age": 20
        )
    }
}

When I run mvn clean package following error always is returned: Failed tests:

  ContractVerifierTest.validate_getTest:26 expected:<[200]> but was:<[404]>

I believe this should be related to the ServletDispatcher as it doesn't see Jersey's paths. The same project with replaced @Path to @RequestMapping works. However, I need to make it working with Jersey. Have I missed something?


回答1:


Have you checked the section about jaxrs support? https://cloud.spring.io/spring-cloud-contract/1.0.x/spring-cloud-contract.html#_jax_rs_support. Here you have an example how you can use it https://github.com/spring-cloud/spring-cloud-contract/tree/1.0.x/spring-cloud-contract-tools/spring-cloud-contract-gradle-plugin/src/test/resources/functionalTest/sampleJerseyProject



来源:https://stackoverflow.com/questions/41198714/spring-cloud-contract-with-jersey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!