gooddata / gooddata-java

GoodData Java SDK
Other
19 stars 63 forks source link

Caused by: com.gooddata.GoodDataRestException: 415: [request_id=xyz] Content type 'application/xml;charset=UTF-8' not supported #837

Open BenDol opened 4 years ago

BenDol commented 4 years ago

Having trouble getting the Afm execution working. Caused by: com.gooddata.GoodDataRestException: 415: [request_id=tzRYVbhAWy3Z8aTS] Content type 'application/xml;charset=UTF-8' not supported

Project insclixProject = goodDataService.getProjectService().getProjectById("XYZ");
ExecuteAfmService executeAfmService = goodDataService.getExecuteAfmService();
MetadataService metadataService = goodDataService.getMetadataService();

Attribute attr = metadataService.getObj(insclixProject, Attribute.class, Restriction.title("PaymentMethod"));
Metric metric = metadataService.getObj(insclixProject, Metric.class, Restriction.title("@NetUnits"));

Afm afm = new Afm()
    // How
    .addAttribute(new AttributeItem(new IdentifierObjQualifier(attr.getDefaultDisplayForm().getIdentifier()), "a1"))
    // What
    .addMeasure(new MeasureItem(new SimpleMeasureDefinition(new UriObjQualifier(metric.getUri())), "m1"));
    // TODO Filter

Execution execution = new Execution(afm);
ExecutionResponse response = executeAfmService.executeAfm(insclixProject, execution);
logger.info(response.getExecutionResultUri());
logger.info(response.toString());

Results in:

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:324)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
    at com.insclix.dashboard.EmbeddedWebApplication.main(EmbeddedWebApplication.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:67)
Caused by: com.gooddata.GoodDataException: Unable to execute AFM
    at com.gooddata.sdk.service.executeafm.ExecuteAfmService.executeAfm(ExecuteAfmService.java:77)
    at com.insclix.dashboard.DevIntitializer.lambda$testData$0(DevIntitializer.java:103)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813)
    ... 10 common frames omitted
Caused by: com.gooddata.GoodDataRestException: 415: [request_id=e0WU2jcSXi0r9LAb] Content type 'application/xml;charset=UTF-8' not supported
    at com.gooddata.sdk.service.util.ResponseErrorHandler.handleError(ResponseErrorHandler.java:55)
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:778)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:414)
    at com.gooddata.sdk.service.executeafm.ExecuteAfmService.executeAfm(ExecuteAfmService.java:71)
    ... 12 common frames omitted

I wasn't able to find any resource on this particular issue. Any help is appreciated.

BenDol commented 4 years ago

Manually overriding the Content-Type header in the settings resolves this issue:

    @Bean
    @ConditionalOnMissingBean
    public GoodDataSettings goodDataSettings() {
        GoodDataSettings settings = new GoodDataSettings();
        settings.setPresetHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
        return settings;
    }

but now I'm getting:

Caused by: com.gooddata.GoodDataRestException: 400: [request_id=wYTzCwTOrOcdjiOw] Malformed request: [Parsing error: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: (<hidden>ResetableServletInputStream); line: 1, column: 2]]
BenDol commented 4 years ago

Okay so the reason the XML content type is added is because the RestTemplate adds a the MappingJackson2XmlHttpMessageConverter message converter that processes the request. So removing this from the RestTemplate message converters resolves the issue. Might need to explicitly define what message converter is used.

jimirocks commented 4 years ago

@BenDol thanks for reporting the issue - just to clarify:

  1. it seems you are using 3.0.0-RC version right?
  2. could you tell us the version of spring framework you use? (I guess they may be changed the default set of converters or so...)
  3. Share us the exact code how do you create the GoodData instance and underlying RestTemplate
liry commented 4 years ago

FYI: ad 1) no, logs say that "GoodData-Java-SDK/2.34.0+api1" version was used

BenDol commented 4 years ago

I changed to use the latest version 3.0.0-RC.3+api3 while trying to resolve it, but @liry is correct I initially was using 2.34.0+api1. The issue occurs in either.

GoodData: 3.0.0-RC.3+api3 Spring: 5.1.5.RELEASE

The reason I'm experiencing this is that the RestTemplate detects what libraries you have in your classpath and add the message converters to the list and for what ever reason the MappingJackson2XmlHttpMessageConverter is being used (likely its just the first in the list before JSON). We need to make sure the RestTemplate favors the MappingJackson2HttpMessageConverter.

RestTemplate:

...

jackson2XmlPresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", classLoader);

...

if (jackson2XmlPresent) {
    this.messageConverters.add(new MappingJackson2XmlHttpMessageConverter());
}

If you want to reproduce the issue just add Maven: com.fasterxml.jackson.dataformat:jackson-dataformat-xml to your project.

jimirocks commented 4 years ago

@BenDol thanks for investigation and sorry for inactivity due to vacancies. We will try to find the way for ensuring the correct set of converters when GoodData is created.

At the same time I suppose you resolved the situation by excluding dataformat-xml from classpath?