0

Goodmorning,

I am trying to generate an API client in Java with Open API. Sadly, it needs to work with java 6. I enable the flag, and everything works fine in the development environment (Eclipse) running on Java 11 with compliance for Java 6. When I export the JAR and try to run it in the staging environment with JRE 1.6, I get the error ClassDefNotFound okhttp3/Interceptor. If I try to download this dependency from Maven I then get Unsupported major.minor version 52.0.

So, I am wondering if the flag supportJava6 actually does something or not, since it seems like it requires at least Java 8.

Am I doing something wrong?

Best regards

UPDATE:

The API specs: https://api.civilianext.it/Protocollo/api/help/v1/docs

The API is authorized with client_ID and secret. I have made a small lib for the authorization, that works with Java6, so the auth is working and done.

What is not working is the API, at runtime usually (it compiles with some quick fixes).

I used both Swagger and OpenAPI, running locally in a docker.

BEWARE: I changed these files many times, I do not remember all combinations and configurations I tried. I do know I tried everything it was posted here in StackOverflow and even more, and nothing worked. I've been here for like a month trying everything when I have time.

These are the latest configurations I tried:

OpenAPI:

version: "3.6"
  services:
    openapi-codegen-cli:
      image: openapitools/openapi-generator-cli:v4.3.1
      volumes:
      - '.:/local'
      command: generate -g jaxrs-spec -i local/in/api.yml -o /local/out/jaxrs-client --additional-properties=dataLibrary=threetenbp,java8=false --skip-validate-spec

volumes:
 openapi_data:

Swagger:

swagger-codegen-cli:
   image: swaggerapi/swagger-codegen-cli
   volumes:
   - '.:/local'
   command: generate -l java -i local/json/api.json -c local/config/config.json -o /local/out/java-client

// config.json

{
  supportJava6: true,
}

Once the code is generated, I usually have to fix the pom.xml, adding <pluginManagement> tags. Then I build the project with mvn clean install. The build compiles and I usually get some errors at runtime, about unsupported major.minor version.

I also tried this: https://github.com/OpenAPITools/openapi-generator/issues/8383

gbos
  • 503
  • 2
  • 6
  • 28
  • It's becoming really hard to find anything still compatible with _Java 6_ today. First because since one year or two, `Java 5` can no more run under recent _Windows_ (I mean : the `java.exe` of _Java 5_) and _Java 6_ sees itself in danger of being rejected too. It has too many vulnerabilities, and among them : 10 or 15 of severity 10 on 10... You should hurry your company to upgrade it in emergency. – Marc Le Bihan Jan 08 '21 at 15:00
  • I totally agree with you but I am alone working on a project from 2005, that needs to run just for a year or so, and they don't care. So I'll waste months of my life to add features and fix things on this mess of a project for just a few months of use lol. – gbos Jan 08 '21 at 18:42

3 Answers3

1

You should use version 1.x of okhttp. Read carefully the change log at https://square.github.io/okhttp/changelog_1x/. Try first with version 1.1.1.

You can download the jar from maven repository at https://mvnrepository.com/artifact/com.squareup.okhttp/okhttp

user2862981
  • 274
  • 1
  • 8
  • I'll try thank you. But I am wondering, why the client is created with okhttp3 if I flagged supportJava6? Also I don't think it will be so straightforward. I can edit the pom.xml and download the older version of http, but I will then need to manually change all the imports in the project. And I am pretty sure the difference between v 1.1.1 and v 3.whatever is huge. Something will brake for sure. But then again, if I have to do this manually and waste hours to make it run on java 6, what's the point of supportJava6=true flag? Mhmhm – gbos Jan 08 '21 at 18:45
  • When the flag support Java6 is checked the byte code generated by the compiler is in compliance with Java6. The library you use okhttp3 is compiled with java8 so it cannot be run with java6. The error 'Unsupported major.minor version 52.0' says that. – user2862981 Jan 08 '21 at 19:07
  • Ok, I can understand that, but it still doesn't make sense to me. If openapi is generating a api client for me, and I ask for java 6 support, but then some critical libraries they're using are not compatible with java 6....what's the point? Which are the use case for "supportJava6" then? – gbos Jan 09 '21 at 09:20
  • Reading https://openapi-generator.tech/docs/generators/java/ flag support of Java6 works well only if client http is jersey1. – user2862981 Jan 09 '21 at 09:34
  • Reading https://stackoverflow.com/questions/36643869/what-jersey-version-i-need-to-download-for-jdk-1-6 seems that you can use also jersey 2 but you have to downgrade it (in the pom.xml) to version 2.6. – user2862981 Jan 09 '21 at 09:44
  • uh ok I read the same documentation and I though supportJava6 flag automatically switched to jersey1. I need to try to force this. I'll check this on Monday, could be really useful. Thank you! – gbos Jan 09 '21 at 11:49
  • So I tried using jersey1 and the situation is better but I still get Unsupported major.minor version 51.0 from Jackson/databind/JsonDeserializer. Jackson version is 1.19.4, how is this possible since until 2.6 Jackson is compile with JDK 6? I am going crazy with this project for real now. – gbos Jan 11 '21 at 08:46
  • Jackson 1 uses java 1.5 see https://github.com/FasterXML/jackson-1/blob/1.9/1.9.9/build.xml. For a maven artifact, see https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl/1.9.13 – user2862981 Jan 11 '21 at 09:18
  • Jackson/databind/JsonDeserializer is a component of jackson 2. Try release 2.9 https://github.com/FasterXML/jackson/wiki/Jackson-Release-2.9. – user2862981 Jan 11 '21 at 09:30
  • I am totally ok if jackson1 uses 1.5, since I need to make it run in 1.6. But then the package Jackson.databind.JsonDeserializer is needed so now obviously I need to run the version 2.6, that should be the last one supporting java6. OpenAPI generated the client for Jackson1 though, so I tried to generate the client again with jackson2. I obviously get other errors now: getOrDefault is used but requires java 8. No problem, fixed with an if statement. Then at runtime I get Jackson/databind/ISO8601DateFormat: Unsupported major.minor version 51.0. – gbos Jan 11 '21 at 12:06
  • Seems like everything I do doesn't. OpenAPI always uses some libraries that require at least java7 or 8. No way to make it work with Java6 without a major manual rewrite of the code probably. – gbos Jan 11 '21 at 12:07
  • You can try Swagger CodeGen (https://github.com/swagger-api/swagger-codegen/tree/3.0.0). I live this link as a reference https://developers.exlibrisgroup.com/blog/building-client-sdks-with-swagger-codegen/ – user2862981 Jan 13 '21 at 13:29
  • There is a risk you may have to rebuild many libraries with Java 6 as compilation target... if source is still compatible to do so. Sounds like even a larger/useless effort than upgrading your project at least to Java 8. – Yves Martin Jan 18 '21 at 12:27
1

It is not obvious to extend an old project with a complete REST stack when Java 6 is a target constraint.

I would propose to give a try to alternate OpenAPI generator JAX-RS spec or JAX-RS CXF client with the hope it is easier to configure Maven project with last Java 6 compatible CXF 3.0.6 version or any other JAX-RS implementation you may find for Java 6.

According to documentation, you have to take care to options dataLibrary=threetenbp and java8=false which should work in version 4.3.1 (Java 6 support has been dropped in version 5.x)

I recommend to follow this tutorial provided with a demonstration project: https://guntherrotsch.github.io/blog_2020/openapi-jaxrs-context.html

Yves Martin
  • 10,217
  • 2
  • 38
  • 77
  • I tried this but somehow jars-cxf generator cannot generate my client from the api.yml (same yml that works for java-client). Dunno why, it creates every class but the API class is just empty methods, full of TODOs – gbos Jan 19 '21 at 09:45
  • @GiammarcoBoscaro I probably did a mistake to recommend CXF specific generator. Please give a try to generic jaxrs-spec generator, maybe running the tutorial from demonstration project first. Even if generated code is standard JAX-RS without CXF specific support, it should run on CXF or any JAX-RS implementation you may get for Java 6. Reminder: do not forget to assign StackOverflow Bounty. – Yves Martin Jan 19 '21 at 10:18
  • @GiammarcoBoscaro As pity you lost your points in not-assigned bounty – Yves Martin Jan 19 '21 at 16:38
  • well I don't know how bounties work, and I don't care about the points anyway. I was looking for a solution and I didn't find it yet so... time to write the API all alone with only Java6 dml – gbos Jan 19 '21 at 16:59
  • Here are details about Bounty: https://meta.stackexchange.com/help/bounty – Yves Martin Jan 21 '21 at 11:36
  • I have found a demonstration project that may help to start... Or else you should publish a MCVE so that we can investigate error messages and tool configurations. https://stackoverflow.com/help/minimal-reproducible-example – Yves Martin Jan 21 '21 at 11:40
  • ok the code I used for the generator is very simple, and the api is public, so I'll post everything when I am back in the office – gbos Jan 21 '21 at 17:36
  • I updated the OP with the code and api specs – gbos Jan 22 '21 at 09:56
  • You have to work on generated pom.xml to provide CXF 3.0.6 as JAX-RS implementation – Yves Martin Jan 23 '21 at 17:46
  • the thing about the generated jax code is that is missing some implementation. Like many classes were empty, full of just some // TODOs – gbos Jan 23 '21 at 18:03
1

Only the jersey1 library in the Java client generator support the supportJava6 option. Here is an example:

npm install -g @openapitools/openapi-generator-cli
openapi-generator-cli generate \
   -i https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml \
   -g java \
   --library jersey1 \
   --additional-properties supportJava6=true \
   -o /var/tmp/java_api_client

FYI. The supportJava6 option has been deprecated and will be removed in the upcoming 5.x release of OpenAPI Generator

William Cheng
  • 10,137
  • 5
  • 54
  • 79
  • I'll try this asap ty – gbos Jan 26 '21 at 10:38
  • As expected I get unsupported major minor version with JsonDeserializer. – gbos Jan 27 '21 at 08:18
  • jackson's dependency versions have been updated to address security issues and only version before 2.8 supports JDK 1.6: https://github.com/intuit/QuickBooks-V3-Java-SDK/issues/51#issuecomment-504336876. You will need to use an old version of OpenAPI Generator to generate code that works right away with 1.6 or you can update the output manually with 2.7.9.5 – William Cheng Jan 27 '21 at 09:14