我正在尝试将一个 int 从 Java 客户端传递给 WebService Rest。我已经看到@PathParam 可以用来传递一个参数,所以我把它放在了WebService 中。
@GET
@Path("/tipusMoviment/{moviment}")
@Produces(MediaType.TEXT_PLAIN)
public int tipusMoviment(@PathParam("moviment") int moviment) {
int result = 99; //si falla el if
if (moviment == 1) {
result = 5;
}else{
result = 3;
}
return result;
}
现在在客户端我有以下内容:
public class NewJerseyClient {
private WebTarget webTarget;
private Client client;
private static final String BASE_URI = "http://localhost:8080/ProyectoREST1/webresources";
public NewJerseyClient() {
client = javax.ws.rs.client.ClientBuilder.newClient();
webTarget = client.target(BASE_URI).path("generic");
}
public <T> T tipusMoviment(Class<T> responseType, String moviment) throws ClientErrorException {
return webTarget.path(java.text.MessageFormat.format("tipusMoviment/{0}", new Object[]{moviment})).request().post(null, responseType);
}
public void close() {
client.close();
}
}
主要是
public class ProyectoCliente7 {
public static void main(String[] args) {
NewJerseyClient ws = new NewJerseyClient();
System.out.println(ws.tipusMoviment(2));
}
}
我不知道我做错了什么或问题是什么,我是 REST 新手,我希望能得到一些帮助,因为尽管我在互联网上搜索,但我无法停止寻找不起作用的东西或我不明白。
有人告诉我,使用 ws.tipusMoviment(2) 必须工作,但在互联网上我发现这样的事情:
Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8080/your-app/rest/data/post");
String input = "{\"message\":\"Hello\"}";
ClientResponse response = webResource.type("application/json")
.post(ClientResponse.class, input);
if (response.getStatus() != 201) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
非常感谢您的宝贵时间。
您可以尝试更改符号:
@Produces(MediaType.TEXT_PLAIN)
经过 :
@Produces(MediaType.APPLICATION_JSON)
有了它,它应该可以工作。
我设法通过了这个号码,为此我使用了:
客户
毛衣
网络服务 REST