Java 使用Activiti Rest web服务时出现问题

Java 使用Activiti Rest web服务时出现问题,java,jersey,activiti,Java,Jersey,Activiti,编辑:为了简化我的问题,有人与我沟通过吗 使用rest?的Activiti,如果是这样,您可以发布您的代码。 谢谢 我花了一段时间才使用Rest登录Activiti。我遵循api指南并实现了以下内容 代码: 请你指出我做错了什么 请注意,当我将该类型替换为“application/json”时,会出现以下错误: 代码: 非常感谢,请尝试以下操作: 在依赖项中包含json模块 创建一个名为LoginInfo的新类,用@XmlRootElement注释,该类有两个公共字段-userId和passw

编辑:为了简化我的问题,有人与我沟通过吗 使用rest?的Activiti,如果是这样,您可以发布您的代码。 谢谢

我花了一段时间才使用Rest登录Activiti。我遵循api指南并实现了以下内容

代码:

请你指出我做错了什么

请注意,当我将该类型替换为“application/json”时,会出现以下错误:

代码:

非常感谢,

请尝试以下操作:

  • 在依赖项中包含json模块
  • 创建一个名为LoginInfo的新类,用@XmlRootElement注释,该类有两个公共字段-userId和password
  • 使用正确的用户ID和密码初始化LoginInfo类实例
  • 将其传递给登录调用
以下是LoginInfo类的代码:

@XmlRootElement
public class LoginInfo {
    public String userId;
    public String password;
}
下面是main()方法的代码:


注意:我还没有试过这个,可能有一些打字错误。此外,LoginInfo还可以变成一个真正的bean,其中包含setter/getter等,只是希望保持简单。查看它是否有效…

作为jersey的替代方案,此代码使用Rest与Activiti交互

此代码来自。所有的功劳都应该归于Tijs Rademakers

public class ActivitiRestClient {

    private static String REST_URI = "http://localhost:8080/activiti-rest/service";
    private static Logger logger = Logger.getLogger(ActivitiRestClient.class);

    private static ClientResource getClientResource(String uri) {
        ClientResource clientResource = new ClientResource(uri);
        clientResource.setChallengeResponse(ChallengeScheme.HTTP_BASIC,
                "kermit", "kermit");
        return clientResource;
    }
....
}
错误

原因:com.sun.jersey.api.client.clienthandler异常:一条消息 Java类型、类的正文编写器 com.sun.jersey.core.util.MultivaluedMapImpl和MIME媒体类型, 在中找不到应用程序/x-www-form-urlencoded com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:299) 在 com.sun.jersey.client.urlconnection.URLConnectionClientHandler.\u调用(URLConnectionClientHandler.java:203) 在 com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:146) ... 8个以上

也可以将其根放在maven依赖项中。在我的例子中,我最终替换了所有的单件球衣,比如

  • 泽西服务器
  • 泽西json
  • 泽西岛客户
带有一个
球衣束

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-bundle</artifactId>
        <version>1.14</version>
    </dependency>

泽西岛
球衣束
1.14

虽然这是一篇老文章,但我想我应该分享我基于HTTPClient 4.1.3版的代码。我在让身份验证机制与Activiti REST一起工作时遇到了类似的问题。虽然本文介绍了基于Jersey和Restlet的解决方案,但我认为发布基于HTTPClient的解决方案会有所帮助

创建HTTPClient。请注意,它还会创建TargetHost(用于执行操作:

public HttpClient createHttpClient() {

targetHost = new HttpHost(REST_HOST_NAME, REST_PORT, REST_PROT);

Credentials defaultcreds = new UsernamePasswordCredentials(USERNAME, PWD);

AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());

HttpClient client = new DefaultHttpClient();
((DefaultHttpClient)client).getCredentialsProvider().setCredentials(authScope, defaultcreds);
 return client;
}
创建HTTP上下文:

public BasicHttpContext createLocalContext() {
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();

// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
// Add AuthCache to the execution context
BasicHttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
return localContext;
}
添加获取目标主机的方法:

public HttpHost getTargetHost() {
//created in the createHttpClient Method
return this.targetHost;
}
最后调用其余部分:

public JSONObject invokeGetWS() throws ClientProtocolException, IOException {
ResponseHandler<String> responseHandler = new BasicResponseHandler();

HttpClient client = createHttpClient();

HttpGet httpGet = new HttpGet(ws_url); 
//ws_url is created at run time e.g http://localhost:9090/activiti-rest/service/user/kermit
System.out.println("executing request: " + httpGet.getRequestLine());

String responseBody = client.execute(getTargetHost(), httpGet, responseHandler, createLocalContext());
System.out.println("----------------------------------------");
System.out.println(responseBody);
System.out.println("----------------------------------------");

return new JSONObject(responseBody);
}
public JSONObject invokeGetWS()抛出ClientProtocolException,IOException{
ResponseHandler ResponseHandler=新BasicResponseHandler();
HttpClient=createHttpClient();
HttpGet HttpGet=新的HttpGet(ws_url);
//ws_url是在运行时创建的,例如http://localhost:9090/activiti-rest/service/user/kermit
System.out.println(“正在执行请求:+httpGet.getRequestLine());
String responseBody=client.execute(getTargetHost()、httpGet、responseHandler、createLocalContext());
System.out.println(“--------------------------------------------------------”;
系统输出打印LN(响应库);
System.out.println(“--------------------------------------------------------”;
返回新的JSONObject(responseBody);
}
我想变量名是不言自明的。
谢谢

我也遇到了同样的问题,通过删除jersey-multipart-1.19.jar上的依赖关系解决了这个问题。当构建一个包含jersey捆绑包和multipart jar的公共依赖关系jar时,我遇到了上述异常。将它们分开解决了这个问题。

请参阅我关于如何做的回复。关于你在上面犯了什么错误-seems Activiti需要json,您正在发送表单。稍后您将更改为json,但Jersey不知道如何将多值映射序列化为json-这就是为什么您必须按照我在下面的回答中的建议创建bean。感谢您的帮助,它成功了,我对依赖项也有问题,它们没有正确导入。。。
public HttpClient createHttpClient() {

targetHost = new HttpHost(REST_HOST_NAME, REST_PORT, REST_PROT);

Credentials defaultcreds = new UsernamePasswordCredentials(USERNAME, PWD);

AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());

HttpClient client = new DefaultHttpClient();
((DefaultHttpClient)client).getCredentialsProvider().setCredentials(authScope, defaultcreds);
 return client;
}
public BasicHttpContext createLocalContext() {
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();

// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
// Add AuthCache to the execution context
BasicHttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
return localContext;
}
public HttpHost getTargetHost() {
//created in the createHttpClient Method
return this.targetHost;
}
public JSONObject invokeGetWS() throws ClientProtocolException, IOException {
ResponseHandler<String> responseHandler = new BasicResponseHandler();

HttpClient client = createHttpClient();

HttpGet httpGet = new HttpGet(ws_url); 
//ws_url is created at run time e.g http://localhost:9090/activiti-rest/service/user/kermit
System.out.println("executing request: " + httpGet.getRequestLine());

String responseBody = client.execute(getTargetHost(), httpGet, responseHandler, createLocalContext());
System.out.println("----------------------------------------");
System.out.println(responseBody);
System.out.println("----------------------------------------");

return new JSONObject(responseBody);
}
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;
import org.restlet.data.ChallengeScheme;
import org.restlet.data.MediaType;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;

public static boolean getAuthenticationSuccess(String username, String password) throws Exception {
    String uri = REST_URI + "/login";
    JSONStringer jsRequest = new JSONStringer();
    jsRequest.object();
    jsRequest.key("userId").value(username);
    jsRequest.key("password").value(password);
    jsRequest.endObject();
    Representation rep = new JsonRepresentation(jsRequest);
    rep.setMediaType(MediaType.APPLICATION_JSON);
    ClientResource clientResource = new ClientResource(uri);

    try {
        JSONObject jsObj = new JSONObject(clientResource.post(rep).getText());
        return jsObj.getBoolean("success");
    } catch (Exception e) {
        // TODO: handle exception
        logger.info("Erreur " + e.getMessage());

        return false;
    }
}