Java Spring as a client for Akka based REST HTTP c

2019-02-27 02:34发布

问题:

i have to call this REST service written in scala-akka project from java-spring.

my scala REST service is like

val route =
    post {
        path("notification" / "signUp"){
            headerValueByName("App_Key") { app_key => {
              handleWith {
                requestParameters: RequestParameters =>
                  //application specific implementation

              }
            }
            }
        }

which contains App_Key and Content-Type in header and request parameters in json format.

request parameters are like:

case class RequestParameters (
     var name: String,
     var email: String,
     var password: String,
     var hashKey: String
   )

so i have to call this REST service from java spring. I an struggling while calling http://ipadress:port/notification/signUp from java .

回答1:

U can call this via. Following Implementation :

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();
        }