Java RESTful Client can't call api when running on server

旧城冷巷雨未停 提交于 2020-08-20 08:09:20

问题


I have a RESTful service running on a Java 14 Spring Boot microservice. I call this from a Client running on a Java 7 Application.

+----------------------+          +-------------------------------+
|  Java 7 REST Client  |  ---->   |  Java 14 Spring Boot Service  |
+----------------------+          +-------------------------------+

I have a RESful Client that calls the api. When I call it from a test main method, it works successfully, and I get the expected results and no errors on client and server.

Problem

When I start the Java 7 (Struts) app in JBoss, and call the api via the same RESTful client, I get errors.

On the Java 14 Server:

java.lang.StackOverflowError: null
    at com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer.findName(ByteQuadsCanonicalizer.java:576) ~[jackson-core-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.findName(UTF8StreamJsonParser.java:2266) ~[jackson-core-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.parseLongName(UTF8StreamJsonParser.java:1875) ~[jackson-core-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.parseMediumName2(UTF8StreamJsonParser.java:1839) ~[jackson-core-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.parseMediumName(UTF8StreamJsonParser.java:1796) ~[jackson-core-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._parseName(UTF8StreamJsonParser.java:1731) ~[jackson-core-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:761) ~[jackson-core-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:646) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:893) ~[jackson-databind-2.11.0.jar:2.11.0]
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:652) ~[jackson-databind-2.11.0.jar:2.11.0]

On the Java 7 Struts App Client:

17:13:48,498 ERROR [stderr] (http--0.0.0.0-8443-1) Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JsonMappingException$Reference
17:13:48,499 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:541)
17:13:48,499 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:143)
17:13:48,499 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:464)
17:13:48,499 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:525)
17:13:48,499 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:143)
17:13:48,499 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:464)
17:13:48,499 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:525)
17:13:48,499 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:143)
17:13:48,499 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:464)
17:13:48,500 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:525)
17:13:48,500 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:143)
17:13:48,500 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:464)
17:13:48,500 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:525)
17:13:48,500 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:143)
17:13:48,500 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:464)
17:13:48,500 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:525)
17:13:48,500 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:143)
17:13:48,500 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:464)
...

I think the issue is because the classpath is different when the Struts Java 7 Client is running in JBoss as opposed from the main.

Question

How can I fix this and call the api successfully when running in JBoss?

I have tried adding the following dependencies to the client and server, it makes not difference.

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.11.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.11.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.11.1</version>
    </dependency>

More info:

Java 7 REST client:

private static ApprovalResponse callSubmitApprovals(String endpointUrl, ApprovalRequest approvalRequest, String token) {
    //System.out.println(endpointUrl);
    try {
        ClientResource cr = new ClientResource(endpointUrl);
        ChallengeResponse challengeResponse = new ChallengeResponse(ChallengeScheme.HTTP_OAUTH_BEARER);
        challengeResponse.setRawValue(token);
        cr.setChallengeResponse(challengeResponse);

        Request req = cr.getRequest();
        Series<Header> headerValue = new Series<>(Header.class);
        req.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, headerValue);

        Representation representation = cr.post(approvalRequest);
        String json = representation.getText();
        //System.out.println(json);

        // now convert the response to java
        ObjectMapper objectMapper = new ObjectMapper();
        ApprovalResponse approvalResponse = objectMapper.readValue(json, ApprovalResponse.class);
        //System.out.println(approvalResponse);
        System.out.println(approvalResponse.getApprovalId());

        return approvalResponse;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

It fails on this line:

Representation representation = cr.post(approvalRequest);

Spring Boot (Java 14) Resource:

@RestController
public class ApprovalSubmitResource {

    @Autowired
    ApprovalSubmitService approvalSubmitService;

    @PostMapping(value = "/rest/approvals-submit", consumes=MediaType.APPLICATION_JSON_VALUE, produces={MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
    public @ResponseBody ResponseEntity<ApprovalResponse> submit(@RequestHeader(name="Authorization") String token, @RequestBody ApprovalRequest approvalRequest) {
        ApprovalResponse approvalResponse = approvalSubmitService.submit(approvalRequest);
        approvalResponse.setApprovalId("test id from server");
        return ResponseEntity.ok(approvalResponse);
    }
}

More info:

I found what the problem is, but I am not sure how to fix it.

When I do the following:

        Gson gson = new Gson();
        String approvalRequestJsonStr = gson.toJson(approvalRequest);

With the approvalRequest when I run JBoss, there's an exception converting the object to JSON.

Caused by: java.lang.StackOverflowError
18:40:26,836 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.google.gson.stream.JsonWriter.peek(JsonWriter.java:350)
18:40:26,836 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.google.gson.stream.JsonWriter.beforeName(JsonWriter.java:565)
18:40:26,836 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:386)
18:40:26,836 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.google.gson.stream.JsonWriter.value(JsonWriter.java:402)
18:40:26,836 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.google.gson.internal.bind.TypeAdapters$13.write(TypeAdapters.java:353)
18:40:26,836 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.google.gson.internal.bind.TypeAdapters$13.write(TypeAdapters.java:337)
18:40:26,836 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
18:40:26,836 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
18:40:26,837 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
18:40:26,837 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.google.gson.internal.bind.ObjectTypeAdapter.write(ObjectTypeAdapter.java:105)
18:40:26,837 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
18:40:26,837 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
18:40:26,837 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
18:40:26,837 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
18:40:26,837 ERROR [stderr] (http--0.0.0.0-8443-1)  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
...

回答1:


I solved this by not using json, but rather xml as the payload:

Client

    try {
        ClientResource cr = new ClientResource(endpointUrl);
        ChallengeResponse challengeResponse = new ChallengeResponse(ChallengeScheme.HTTP_OAUTH_BEARER);
        challengeResponse.setRawValue(token);
        cr.setChallengeResponse(challengeResponse);

        Request req = cr.getRequest();
        Series<Header> headerValue = new Series<>(Header.class);
        req.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, headerValue);

        XStream xstream = new XStream();
        xstream.alias("approvalRequest", ApprovalRequest.class);
        String xmlString = xstream.toXML(approvalRequest);

        ApprovalResponse approvalResponse = cr.post(xmlString, ApprovalResponse.class);

        System.out.println(approvalResponse.getApprovalId());

        return approvalResponse;
    } catch (Exception e) {
        e.printStackTrace();
    }

Server

@PostMapping(value = "/rest/approvals-submit", consumes=MediaType.APPLICATION_JSON_VALUE, produces={MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
public @ResponseBody ResponseEntity<ApprovalResponse> submit(@RequestHeader(name="Authorization") String token, @RequestBody String approvalRequestXML) {
    System.out.println("ApprovalSubmitResource: "+approvalRequestXML);
    XStream xstream = new XStream();
    xstream.allowTypesByWildcard(new String[] {
            "com.nexct.approvalservice.**"
    });
    xstream.alias("approvalRequest", ApprovalRequest.class);
    ApprovalRequest approvalRequest = (ApprovalRequest) xstream.fromXML(approvalRequestXML);

    System.out.println("ApprovalSubmitResource approvalRequest: "+approvalRequest);
    System.out.println("ApprovalSubmitResource: "+approvalRequest.getMessageToEvaluator());
    //ApprovalResponse approvalResponse = approvalSubmitService.submit(approvalRequest);
    ApprovalResponse approvalResponse = new ApprovalResponse();
    System.out.println("ApprovalSubmitResource: "+approvalResponse);
    approvalResponse.setApprovalId("test id from server");
    return ResponseEntity.ok(approvalResponse);
}


来源:https://stackoverflow.com/questions/62597822/java-restful-client-cant-call-api-when-running-on-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!