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

问题描述 投票:1回答:2

我正在开发一个REST客户端来连接到我无法访问的外部服务器。我已经能够使用Postman和SOAPUi成功地建立通信,现在我正在尝试使用Java(org.glassfish.jersey.client.ClientResponse),但无济于事,因为每当我包含“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)

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

提前致谢。

java rest post jersey
2个回答
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<>()


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-

© www.soinside.com 2019 - 2024. All rights reserved.