REST客户端Content-Type标头抛出UnsupportedOperationException

时间:2018-03-05 20:40:24

标签: java rest post jersey

我正在开发一个REST客户端来连接到我无法访问的外部服务器。我已经能够使用Postman和SOAPUi成功地建立通信,现在我尝试使用Java(org.glassfish.jersey.client.ClientResponse),但无济于事,因为每当我加入&#34时; Content-type:application / json"标题,我得到一个例外,如果我不包含此标题,我会收到415错误(不支持的媒体类型),我相信这要求我包含Content-Type标题。

这是标题准备:

Client client = ClientBuilder.newClient();

MultivaluedMap<String, Object> headerMap = new MultivaluedHashMap<String, Object> ();

String username="admin";
String password="admin";
String usernameAndPassword = username + ":" + password;
String authorizationHeaderValue = "Basic " + java.util.Base64.getEncoder().encodeToString( usernameAndPassword.getBytes() );
headerMap.put("Authorization", Arrays.asList(new Object [] { authorizationHeaderValue }));
//headerMap.put("Content-Type", Arrays.asList(new Object [] { "application/json" }));
headerMap.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(new Object [] { MediaType.APPLICATION_JSON }));

身体内容的一部分:

JsonObject value = Json.createObjectBuilder()
    .add("var1", var1Object)
    .add("var2", var2Object)
    .add("var3", var3Array)
    .add("var4", var4Array)
    .build();

post方法调用:

Response response = client
    .target("https://example.com")
    .request("application/json")
    .headers(headerMap)
    .post(Entity.text(value.toString()));

我得到的错误:

Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(Unknown Source)
at java.util.AbstractList$Itr.remove(Unknown Source)
at java.util.AbstractList.removeRange(Unknown Source)
at java.util.AbstractList.clear(Unknown Source)
at javax.ws.rs.core.AbstractMultivaluedMap.putSingle(AbstractMultivaluedMap.java:97)
at org.glassfish.jersey.message.internal.OutboundMessageContext.setMediaType(OutboundMessageContext.java:708)
at org.glassfish.jersey.client.ClientRequest.type(ClientRequest.java:419)
at org.glassfish.jersey.client.ClientRequest.variant(ClientRequest.java:443)
at org.glassfish.jersey.client.JerseyInvocation$Builder.storeEntity(JerseyInvocation.java:179)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:427)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:334)
at main.java.ofbizrestclientdemo.Main.testCreateProduct(Main.java:438)
at main.java.ofbizrestclientdemo.Main.main(Main.java:520)

我一直试图找到这个问题的原因,但无济于事。任何人都可以看出是什么激发了这个错误?

提前致谢。

2 个答案:

答案 0 :(得分:0)

更改

.POST(Entity.text(value.toString()));

.POST(Entity.json(value.toString()));

在您的客户电话中。 Entity.text生成“text / plain”内容类型。

Doc:https://docs.oracle.com/javaee/7/api/javax/ws/rs/client/Entity.html#text-T-

答案 1 :(得分:0)

问题是由headerMap

中的这些值引起的
headerMap.put("Authorization", Arrays.asList(new Object [] { authorizationHeaderValue }));
headerMap.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(new Object [] { MediaType.APPLICATION_JSON }));

Arrays.asList会返回一个不可修改的地图,但AbstractMultivaluedMap.putSingle会尝试清除值列表,但列表不允许这样做,而且会抛出UnsupportedOperationException

我建议将Arrays.asList替换为正常List初始化(new ArrayList<>())。