Edit: to simplify my question, has anyone managed to communicate with Activiti using rest?, and if so could you be kind to post your code. thanks.
我一直在努力使用 Rest 登录 Activiti。我遵循了 api 指南并实现了以下内容
代码:
package demo;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider;
public class Aloha {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Client client = Client.create();
WebResource webResource = client
.resource("http://localhost:8080/activiti-rest/service/login");
MultivaluedMap<String, String> formData = new MultivaluedMapImpl();
formData.add("userId", "kermit");
formData.add("password", "kermit");
ClientResponse response;
try {
response = webResource.type("application/x-www-form-urlencoded")
.post(ClientResponse.class, formData); // webResource.accept(MediaType.TEXT_PLAIN_TYPE).post(ClientResponse.class,
// formData);
System.out.print(response.toString());
} catch (UniformInterfaceException ue) {
System.out.print(ue.getMessage());
}
}
}
如您所见,我正在使用 Jersey 来使用网络服务,这是我一直收到的响应:
引用:
POST http://localhost:8080/activiti-rest/service/login returned a response status of 415 Unsupported Media Type
请你指出我在这里做错了什么?
请注意,当我将类型替换为“application/json”时,出现以下错误:
代码:
Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.sun.jersey.core.util.MultivaluedMapImpl, and MIME media type, application/json, was not found
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:149)
at com.sun.jersey.api.client.Client.handle(Client.java:648)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:670)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:563)
at demo.Aloha.main(Aloha.java:32)
Caused by: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.sun.jersey.core.util.MultivaluedMapImpl, and MIME media type, application/json, was not found
at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:204)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:147)
... 5 more
非常感谢,
最佳答案
尝试以下操作:
- 在你的依赖中包含 jersey-json 模块
- 创建一个名为 LoginInfo 的新类,使用 @XmlRootElement 注释进行注释,具有两个公共(public)字段 - userId 和 password
- 使用正确的用户 ID 和密码初始化 LoginInfo 类实例
- 将其传递给登录调用
这是 LoginInfo 类的代码:
@XmlRootElement
public class LoginInfo {
public String userId;
public String password;
}
这是 main() 方法的代码:
Client client = Client.create();
WebResource webResource = client
.resource("http://localhost:8080/activiti-rest/service/login");
LoginInfo loginInfo = new LoginInfo();
loginInfo.userId = "kermit";
loginInfo.password = "kermit";
ClientResponse response;
try {
response = webResource.type("application/json").post(ClientResponse.class, loginInfo);
System.out.print(response.toString());
} catch (UniformInterfaceException ue) {
System.out.print(ue.getMessage());
}
注意:我没试过,可能有一些错别字。 LoginInfo 也可以变成一个带有 setter/getter 和东西的真正的 bean,只是想保持简单。看看它是否有效...
关于java - 使用 Activiti Rest Web 服务时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7467285/