1

我有以下json来解析 int JSONObject并通过 http 客户端发送它以使用其REST API在OIM 11g PS3中创建用户。

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.parser.ParseException;
public class OimApi {

@SuppressWarnings("deprecation")
    public static void main(String[] args) throws JSONException, ParseException {
        //String url="http://ussltc7146v.dev.sltc.com:14000/idaas/im/scim/v1/Users";        
        //JsonPack oimRequest = new JsonPack();
        //String URL = JsonPack.getJSONString("http://ussltc7146v.dev.sltc.com:14000/idaas/im/scim/v1/Users");

        String jsonString ="{ \"schemas\": [\r\n\"urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User\",\r\n\"urn:ietf:params:scim:schemas:core:2.0:User\",\r\n\"urn:ietf:params:scim:schemas:extension:oracle:2.0:IDM:User\",\r\n\"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User\"\r\n],\r\n\"userName\": \"ECLIPSE\",\r\n\"password\":\"Passw0rd\",\r\n\"name\": {\r\n\"givenName\": \"first\",\r\n\"familyName\": \"last\"\r\n},\r\n\"emails\": [\r\n{ \"value\": \"antiktest_eclipse@edd.ca.gov\",\r\n\"type\": \"work\"\r\n}\r\n],\r\n\"userType\": \"Full-Time\",\r\n\"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User\": {\r\n\"organization\": \"Xellerate Users\"\r\n}\r\n}";

        JSONObject json= new JSONObject(jsonString);
        HttpClient httpClient = HttpClientBuilder.create().build();  

        try {
             HttpHost proxy = new HttpHost("127.0.0.1", 9998, "http");
            DefaultHttpClient httpclient = new DefaultHttpClient();
                try {
                    httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
                }
                finally{

                }

            HttpPost request = new HttpPost("URL for OIM REST API");
            StringEntity params =new StringEntity(json.toString());
            request.addHeader("content-type", "application/json");
            request.setEntity(params);
            HttpResponse response = httpClient.execute(request);
            System.out.println("response is "+ response);
            // handle response here...
        }catch (Exception ex) {

            ex.printStackTrace();
        } finally {
            httpClient.getConnectionManager().shutdown(); //Deprecated
        }   


    }

我收到以下错误消息:

  response is HttpResponseProxy{**HTTP/1.1 415 Unsupported Media Type** [Date: Thu, 01 Oct 2015 05:17:44 G                    MT,
  Transfer-Encoding: chunked, Content-Type: text/plain,
  X-ORACLE-DMS-ECID: cc6ca29dd5361a5c:-25d24                   
  3fd:150213338cb:-8000-00000000000003aa, X-Powered-By: Servlet/2.5
  JSP/2.1] ResponseEntityProxy{[Content-Type: text/plain,Chunked: true]}}

在此处输入图像描述

4

2 回答 2

2

根据您收到HTTP 415 Unsupported Media Type 的错误,您在发布到 REST 端点时似乎使用了错误的媒体类型。尝试使用:

request.addHeader("content-type", "application/scim+json");

而不是您现在使用的应用程序/json。

还要确保您发布到OIM中 /Users 的正确 REST 端点。

我原以为您用于请求 JSON 的组织是只读属性,您可能无法使用它来写入新用户。您可能必须使用urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User值设置为 act_key 的类型和引用。

于 2016-02-17T05:30:30.207 回答
0

我不知道它是否适合您,但我将 JSON 数据发送到我的服务器并获得如下响应 JSON:

URL url = new URL(/* here is URL such as www.example.com/example */);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestMethod("POST");

        // ObjectMapper isn't used here but it is for sending java class instances in JSON and receive them from JSON
        ObjectMapper mapper = new ObjectMapper();

        JSONObject jsonWriter = new JSONObject();
        jsonWriter.put("token", token);

        OutputStream os = conn.getOutputStream();
        os.write(jsonWriter.toString().getBytes("UTF-8"));
        os.flush();
        os.close();

        System.out.println("Response Code: " + conn.getResponseCode());
        InputStream in = new BufferedInputStream(conn.getInputStream());
        String response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
        //read response by using it
        JSONObject jsonReader = new JSONObject(response);
于 2016-07-03T03:39:36.630 回答