我有以下方法:
@PUT
@Path("/reduceEnergy/{id}/{action}")
String reduceEnergyConsumption(@PathParam("id") int id,
@PathParam("action") String action);
我想从客户端调用此方法。(以防万一,当我有一个GET方法时,我是这样写的:
String response = target.path("air_quality")
.path("reduceEnergy/"+action)
.request()
.accept(MediaType.TEXT_PLAIN)
.get(String.class);
System.out.println(response);
但是现在我有一个PUT方法。我是这样写的:
但我不知道如何完成或纠正它
Response response = target.path("aqsensor")
.path("reduceEnergy/"+pr+"/"+action)
.request()
.accept(MediaType.TEXT_PLAIN)
.put(null);
System.out.println(response.getStatus());
感谢您帮助我找到解决方案。
你不能在 put 中发送null
,你需要发送一个实体
给定终结点的以下定义:
@Path("myresource")
public class MyResource {
@PUT
@Path("/reduceEnergy/{id}/{action}")
public String reduceEnergyConsumption(@PathParam("id") int id,
@PathParam("action") String action) {
System.out.println("id: " + id);
System.out.println("action: " + action);
return "";
}
}
你可以这样做:
Entity<String> userEntity = Entity.entity("", MediaType.TEXT_PLAIN);
Response response = target.path("myresource/reduceEnergy/10/action")
.request()
.put(userEntity);
System.out.println("Status: " + response.getStatus());
这表示:
id: 10
action: action
Status: 200