我必须从 java-spring 调用这个用 scala-akka 项目编写的 REST 服务。
我的 scala REST 服务是这样的
val route =
post {
path("notification" / "signUp"){
headerValueByName("App_Key") { app_key => {
handleWith {
requestParameters: RequestParameters =>
//application specific implementation
}
}
}
}
包含头部的App_Key和Content-Type以及json格式的请求参数。
请求参数如下:
case class RequestParameters (
var name: String,
var email: String,
var password: String,
var hashKey: String
)
所以我必须从 java spring 调用这个 REST 服务。我在从 java 调用 http://ipadress:port/notification/signUp
时遇到了困难。
最佳答案
你可以通过调用它。以下实现:
try {
Client client = Client.create();
WebResource webResource = client.resource(http://ipadress:port/notification/signUp);
JSONObject formData=new JSONObject();
formData.put("name", UserName);
formData.put("email", EmailId);
formData.put("password", Password);
formData.put("urlHash",HashKey);
ClientResponse response = webResource.header("App_Key",xxxxxxxxxxxxxxxxxxxxxxxxxx).type(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class, formData);
} catch (Exception e) {
e.printStackTrace();
}
关于Java Spring 作为基于 Akka 的 REST HTTP 调用的客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38391029/