与泽西岛的Spring Cloud合同 [英] Spring Cloud Contract with Jersey
问题描述
我有一个简单的项目Spring boot项目.它包含一个基于Jersey的Controller: @Path("persons") @Produces(MediaType.APPLICATION_JSON) 公共类PersonsController {
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);
}
}
它按预期返回json响应(URL: http://localhost:PORT/persons ):
It returns json response as expected (url: http://localhost:PORT/persons):
{
"name": "James",
"age": 20
}
我的目标是为此控制器添加Spring Cloud Contract测试.我已经添加了所有必需的mvn配置,并进行了测试:
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());
}
}
以下是合同(常规文件): 导入org.springframework.cloud.contract.spec.Contract
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
)
}
}
当我运行mvn clean package
时,总是返回以下错误:
测试失败:
When I run mvn clean package
following error always is returned:
Failed tests:
ContractVerifierTest.validate_getTest:26 expected:<[200]> but was:<[404]>
我认为这应该与ServletDispatcher有关,因为它看不到Jersey的路径.替换为@RequestMapping的@Path的同一项目有效.但是,我需要使其与Jersey一起使用. 我错过了什么吗?
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?
推荐答案
您是否检查了有关jaxrs支持的部分? https://cloud.spring. io/spring-cloud-contract/1.0.x/spring-cloud-contract.html#_jax_rs_support .这里有一个示例如何使用它
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
这篇关于与泽西岛的Spring Cloud合同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!