Java 为什么RESTful web服务不能正确调用semaphore.acquire()?
我有一个具有以下方法代码的web服务:Java 为什么RESTful web服务不能正确调用semaphore.acquire()?,java,rest,jersey,semaphore,Java,Rest,Jersey,Semaphore,我有一个具有以下方法代码的web服务: private static Semaphore reacted = new Semaphore(0); @Path("/p1") @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public UserDefinedClass postMethod1( UserDefinedClass udc) { If (Condition A)
private static Semaphore reacted = new Semaphore(0);
@Path("/p1")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public UserDefinedClass postMethod1( UserDefinedClass udc) {
If (Condition A)
semaphore.acquire();
System.out.println("Test");
UserDefinedClass u = new UserDefinedClass(udc);
return u;
}
@Path("/p2")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public void postMethod2(UserDefinedClass udc) throws IOException {
...
reacted.release();
}
如果条件A没有出现,服务将正确地响应调用环境(让我们将其命名为CE),但是,如果条件A为true,则方法postMethod1将阻止等待有人调用postMethod2
如果有人呼叫postMethod2(从CE呼叫到postMethod1的1分钟内)。postMethod1被解锁,但是,下面的代码是semaphore.acquire();从不执行,且未打印“测试”消息,而CE中出现以下错误:
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/html;charset=utf-8, type=class...
请注意,如果条件A不正确,CE中不会显示错误。这意味着问题不在于方法定义,而是因为方法的线程进入等待状态,等待信号量
行政长官代码为:
UserDefinedClass udc = new UserDefinedClass();
ClientConfig config = new ClientConfig(JacksonJsonProvider.class);
Client client = ClientBuilder.newClient(config);
String targetUrl = "http://localhost:8080/myApp";
WebTarget target = client.target(targetUrl);
Invocation.Builder invocationBuilder = target.path("rest").
path("p1").
request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.post(Entity.entity(udc, MediaType.APPLICATION_JSON));
UserDefinedClass u = response.readEntity(UserDefinedClass.class);
那到底是什么问题 我不知道如何用您的脚本语言实现,但内容类型
text/html
是错误的
我也不确定它是否是响应或请求部分,但它不匹配或未正确设置APPLICATION\u JSON
您应该能够通过调查真实的网络流量轻松解决这个问题(我使用
ngrep
,但其他人更喜欢tcpdump
)。语言是JAVA。响应类型没有问题,正如我在问题中已经说过的,如果条件A不正确,postMethod1可以顺利工作。因此,如果响应类型中存在问题,它应该根本不起作用。主要问题是postMethod1线程在获取信号量后没有继续执行。我不知道为什么!您在响应中得到text/html,因为服务器上有一个错误,而服务器实际响应的是一个错误的html页面。在调用readEntity之前,最好先检查响应的状态。我打赌是500,这意味着服务器出错。您需要在服务器上进行一些调试。您是否尝试放置断点并查看发生了什么情况?这是由于服务器战争导致的问题,如果(条件a)为真,则返回null。非常感谢。