0

当使用 REST api 使用 jersey java REST 客户端访问 WSO2 DAS 时,我将响应作为不受支持的媒体类型,响应状态为 415 和响应

InboundJaxrsResponse{context=ClientResponse{method=POST, uri= https://localhost:9443/analytics/search , status=415, reason=Unsupported Media Type}}

下面的客户端来源。任何人都可以帮助解决这个问题。


package com.rilfi.research.c2c.das.rest.client;

import org.glassfish.jersey.SslConfigurator;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import javax.net.ssl.SSLContext;`enter code here`
import javax.ws.rs.client.*;
import javax.ws.rs.core.*;

public class DasClientApp {
    public static void main(String[] args) {
        SslConfigurator sslConfig = SslConfigurator.newInstance()
                .trustStoreFile("./client-truststore.jks")
                .trustStorePassword("wso2carbon")
                .keyStoreFile("wso2carbon.jks")
                .keyPassword("wso2carbon");
        SSLContext sslContext = sslConfig.createSSLContext();
        Client client = ClientBuilder.newBuilder().sslContext(sslContext).build();
        WebTarget webTarget = client.target("https://localhost:9443").path("analytics/search");
        MultivaluedMap<String, String> formData = new MultivaluedHashMap<String, String>();
        formData.add("tableName", "V1SALEFULL3");
        formData.add("query", "product:phone");
        formData.add("start", "0");
        formData.add("count", "10");
        Response response = webTarget.request(MediaType.APPLICATION_JSON).post(Entity.form(formData));
        System.out.println(response.getStatus());
        System.out.println(response.readEntity(String.class));
        System.out.println(response);
    }

}
4

1 回答 1

0

添加后HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("admin", "admin"); 我现在可以使用 rest api 访问 wso2 DAS

包 com.rilfi.research.c2c.das.rest.client;

import org.glassfish.jersey.SslConfigurator;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

import javax.net.ssl.SSLContext;
import javax.ws.rs.client.*;
import javax.ws.rs.core.*;

/**
 * Created by rilfi on 5/5/16.
 */
public class DasClientApp {
    public static void main(String[] args) {
        SslConfigurator sslConfig = SslConfigurator.newInstance()
                .trustStoreFile("client-truststore.jks")
                .trustStorePassword("wso2carbon")
                .keyStoreFile("wso2carbon.jks")
                .keyPassword("wso2carbon");
        SSLContext sslContext = sslConfig.createSSLContext();
        HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("admin", "admin");
        Client client = ClientBuilder.newBuilder().sslContext(sslContext).build();
        WebTarget webTarget = client.target("https://localhost:9443").path("analytics/search").register(feature);
        String payload = "{\"tableName\":\"V1SALEFULL3\",\"query\":\"product:phone\",\"start\":50,\"count\":10}";
        Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(payload));
        System.out.println(response.getStatus());
        System.out.println(response.readEntity(String.class));
        System.out.println(response);
    }

}
于 2016-05-05T09:08:10.200 回答