通过restlet API调用RESTful WS

时间:2015-01-29 15:25:56

标签: java web-services restlet

我正在尝试从我的客户端代码(基于Restlet)调用WS,但是我在client.post方法上遇到了错误的请求异常(HTTP状态代码400)。

public static void main(String[] args) throws Exception{

    String serviceUrl = "http://localhost:8080/MPosBEService"+BeServicePath.MERCHANT_REGISTRATION;
    //String serviceUrl = "http://192.168.101.124:8081/be"+BeServicePath.MERCHANT_REGISTRATION;
    ClientResource client = new ClientResource( serviceUrl );

    addHeaders( client );

    objectMapper=new ObjectMapper();
    objectMapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);

    try{
        Form form = new Form();
        form.set("username", "intendanet");
        form.set("password", "1111");

        client.post( form, MediaType.APPLICATION_JSON );//**this line is the culprit**

        //client.post(new  MerchantRegisterRequestPojo("intendanet","1111"), MediaType.APPLICATION_JSON );

    }catch(Exception e) 
    {
        System.out.println(e.getLocalizedMessage());
        throw e;
    }

    Response response = client.getResponse();

    if( response.getStatus().getCode() == 200 )
    {
        m = deserializeJson( (HttpEntity)response.getEntity(), MerchantPojo.class );
        System.out.println("Result:" + m.id);
    }else {
        System.out.println("Exception: "+ response.getStatus().getCode());

        throw new HttpStatusException(response.getStatus().getCode());
    }
}

static private void addHeaders(ClientResource client){

    client.getRequestAttributes().put(BeServiceHTTPHeader.REQ_MPOS_BE_SERVICE_LANGUAGE_ID.toString(), "1"+"");
    client.getRequestAttributes().put(BeServiceHTTPHeader.REQ_MPOS_BE_SERVICE_DEVICE_APP_VERSION.toString(), "1"+"");
    client.getRequestAttributes().put(BeServiceHTTPHeader.REQ_MPOS_BE_SERVICE_DEVICE_GENERATED_ID.toString(), "flskdjf9ksmflskmfl");
    client.getRequestAttributes().put(BeServiceHTTPHeader.REQ_MPOS_BE_SERVICE_DEVICE_INFO.toString(), "ovo je moj device!");

}

static private <T> T deserializeJson(HttpEntity json, Class<T> targetType) throws Exception{
    return objectMapper.readValue(EntityUtils.toString(json), targetType);
}

由于

1 个答案:

答案 0 :(得分:1)

我并不完全是您的REST服务所期望的,但我发现您的代码中存在两个潜在的问题。

首先,标题未在您的回复中正确设置。您应该按照以下说明更新方法addHeaders

static private void addHeaders(ClientResource client){
    Series<Header> headers = (Series<Header>) cr.getRequestAttributes().get(
                                     "org.restlet.http.headers");
    if (headers==null) {
        headers = new Series<Header>(Header.class);
        cr.getRequestAttributes().put(
                "org.restlet.http.headers", headers);
    }

    headers.set(BeServiceHTTPHeader.REQ_MPOS_BE_SERVICE_LANGUAGE_ID.toString(), "1");
    headers.put(BeServiceHTTPHeader.REQ_MPOS_BE_SERVICE_DEVICE_APP_VERSION.toString(), "1");
    headers.put(BeServiceHTTPHeader.REQ_MPOS_BE_SERVICE_DEVICE_GENERATED_ID.to String(), "flskdjf9ksmflskmfl");
    headers.put(BeServiceHTTPHeader.REQ_MPOS_BE_SERVICE_DEVICE_INFO.toString(), "ovo je moj device!");
}

其次,您需要注意发送的内容和相关的媒体类型。你应该有类似的东西:

// With form
Form form = new Form();
form.set("username", "intendanet");
form.set("password", "1111");

client.post(form, MediaType.APPLICATION_FORM_URLENCODED);

// With JSON
JSONObject jsonObj = new JSONObject
jsonObj.put("username", "intendanet");
jsonObj.put("password", "1111");
client.post(new JsonRepresentation(jsonObj));

希望它有所帮助, 亨利