我有netbean Rest Web服务项目,有以下几种方法:
@Path("restws")
公共课RestWs {
@Context
private UriInfo context;
public RestWs() {
}
@GET
@Produces("application/json")
public String getJson() {
return ("{\"pesan\":\"hello\"}");
}
@PUT
@Produces("text/plain")
@Consumes("application/json")
public String putJson(String content) {
return("Content yang didapat : "+content);
}
@Path("/mahasiswaData/{id}")
@GET
@Produces("application/json")
public String getMahasiswaByID(@PathParam("id")String nim)
{
JSONObject jo = new JSONObject();
jo.put("id", nim);
jo.put("nama", "Budi");
return(jo.toJSONString());
}
@Path("/mahasiswaData/{id}")
@POST
@Consumes("text/plain")
@Produces("application/json")
public String postMahasiswaByID(@PathParam("id")String nim, String data)
{
JSONObject jo = new JSONObject();
jo.put("id", nim);
jo.put("nama", "Budi");
jo.put("message", data);
return(jo.toJSONString());
}
@Path("/mahasiswaQuery")
@GET
@Produces("application/json")
public String getMahasiswaQuery(@QueryParam("nim")String nim, @QueryParam("nama") String nama)
{
JSONObject jo = new JSONObject();
jo.put("nim", nim);
jo.put("nama", nama);
jo.put("method", "GET");
return(jo.toJSONString());
}
@Path("/mahasiswaQuery")
@POST
@Produces("application/json")
public String postMahasiswaQuery(@QueryParam("nim")String nim, @QueryParam("nama") String nama)
{
JSONObject jo = new JSONObject();
jo.put("nim", nim);
jo.put("nama", nama);
jo.put("method", "Post");
return(jo.toJSONString());
}
}
然后我创建新项目并添加其余的Web服务客户端。自动netbeans让我成为一个新的类:
public class NewJerseyClient {
private WebTarget webTarget;
private Client client;
private static final String BASE_URI = "http://localhost:8080/PTIRestServer/webresources";
public NewJerseyClient() {
client = javax.ws.rs.client.ClientBuilder.newClient();
webTarget = client.target(BASE_URI).path("restws");
}
public String putJson(Object requestEntity) throws ClientErrorException {
return webTarget.request(javax.ws.rs.core.MediaType.APPLICATION_JSON).put(javax.ws.rs.client.Entity.entity(requestEntity, javax.ws.rs.core.MediaType.APPLICATION_JSON), String.class);
}
public String getMahasiswaByID(String id) throws ClientErrorException {
WebTarget resource = webTarget;
resource = resource.path(java.text.MessageFormat.format("mahasiswaData/{0}", new Object[]{id}));
return resource.request(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(String.class);
}
public String postMahasiswaQuery() throws ClientErrorException {
return webTarget.path("mahasiswaQuery").request().post(null, String.class);
}
public String getMahasiswaQuery(String nim, String nama) throws ClientErrorException {
WebTarget resource = webTarget;
if (nim != null) {
resource = resource.queryParam("nim", nim);
}
if (nama != null) {
resource = resource.queryParam("nama", nama);
}
resource = resource.path("mahasiswaQuery");
return resource.request(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(String.class);
}
public String postMahasiswaByID(Object requestEntity, String id) throws ClientErrorException {
return webTarget.path(java.text.MessageFormat.format("mahasiswaData/{0}", new Object[]{id})).request(javax.ws.rs.core.MediaType.TEXT_PLAIN).post(javax.ws.rs.client.Entity.entity(requestEntity, javax.ws.rs.core.MediaType.TEXT_PLAIN), String.class);
}
public String getJson() throws ClientErrorException {
WebTarget resource = webTarget;
return resource.request(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(String.class);
}
public void close() {
client.close();
}
}
我可以使用以下内容轻松访问所有get方法:
public static void main(String[] args) {
System.out.println(new NewJerseyClient().getMahasiswaQuery("23", "John"));
}
但是当我尝试使用此代码访问post / put方法时:
public static void main(String[] args) {
NewJerseyClient c = new NewJerseyClient();
System.out.println(c.putJson("{\"name\":\"john\"}"));
System.out.println(c.postMahasiswaQuery());
System.out.println(c.postMahasiswaByID("plain text", "1"));
}
所有的方法调用都给了我一个例外:
Exception in thread "main" javax.ws.rs.NotAcceptableException: HTTP 406 Not Acceptable
at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:898)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:749)
at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:88)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:650)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:421)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:646)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:402)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:305)
at client.NewJerseyClient.postMahasiswaByID(NewJerseyClient.java:68)
at client.NewJerseyClient.main(NewJerseyClient.java:84)
谁能帮助我吗?如何访问方法put / post?或者任何人都有一个示例代码如何访问这些方法?
感谢
答案 0 :(得分:0)
您的Rest服务代码看起来不错。然而,Netbeans中的客户端代码生成有一个问题,我在netbeans中提交了bug。
使您的客户端代码成功。请将 putJson 请求内容类型从 javax.ws.rs.core.MediaType.APPLICATION_JSON 更改为 javax.ws.rs.core.MediaType.TEXT_PLAIN
public String putJson(Object requestEntity) throws ClientErrorException {
return webTarget.request(javax.ws.rs.core.MediaType.TEXT_PLAIN)
.put(javax.ws.rs.client.Entity.entity(requestEntity, javax.ws.rs.core.MediaType.APPLICATION_JSON), String.class);
}
同样,对于 postMahasiswaByID 方法,请将内容类型 javax.ws.rs.core.MediaType.TEXT_PLAIN 更改为 javax.ws.rs.core。 MediaType.APPLICATION_JSON
public String postMahasiswaByID(Object requestEntity, String id) throws ClientErrorException {
return webTarget.path(java.text.MessageFormat.format("mahasiswaData/{0}", new Object[]{id}))
.request(javax.ws.rs.core.MediaType.APPLICATION_JSON)
.post(javax.ws.rs.client.Entity.entity(requestEntity, javax.ws.rs.core.MediaType.TEXT_PLAIN), String.class);
}
如果有效,请告诉我。
由于 vidhya