我正在尝试使用
javax.ws.rs.client.Client
来调用另一个服务。当请求/响应中有蛇形外壳时,我没有得到正确的响应。代码在服务器上运行时运行良好,但在 Junit 中尝试使用客户端调用服务时就不行了。
示例代码:
@BeforeEach
public void init() {
wm1.stubFor(
post("/customers")
.willReturn(
ok().withHeader("Content-type", "application/json")
.withBody(
"""
{"customer_name": "Test1",
"customer_id": "71234"
}""")));
}
@Test
void buildClientTest() {
Jsonb jsonB =
JsonbBuilder.newBuilder()
.withConfig(
new JsonbConfig()
.withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES))
.build();
Client client = ClientBuilder.newBuilder().register(jsonB).build();
CustomerResponse customerResponse =
client
.target("http://localhost:8089/customers")
.request(MediaType.APPLICATION_JSON)
.header("accept", "application/json")
.post(
Entity.entity(buildRequest(), MediaType.APPLICATION_JSON), CustomerResponse.class);
System.out.println("The response is " + customerResponse.toString());
String customerId = customerResponse.getCustomerId();
assertEquals("71234", customerId);
}