SwaggerHub's generated spring boot server stub gives 404's when ran - java

I created a sample OpenAPI spec using SwaggerHub:
https://app.swaggerhub.com/apis/Leejjon/Example/1.0.0
Then I generate a server stub via Export -> Server Stub -> Spring (you can do the same via the url above to get my full code)
I am running on Windows 7 (don't laugh) and Java 11. When doing a mvn clean install I get:
C:/Users/Leejjon/Downloads/spring-server-generated/src/main/java/io/swagger/api/ApiResponseMessage.java:[3,33] package javax.xml.bind.annotation does not exist
This is because Jaxb has been removed since Java 11, so I add the Jaxb API and an implementation to my pom.xml:
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.4.0-b180830.0359</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.4.0-b180830.0438</version>
</dependency>
Now everything compiles and I can run the server using mvn spring-boot:run -Dserver.port=8080. Everything seems fine. The server seems to boot up and logging seems to indicate that it's listening for requests.
See full output here: https://pastebin.com/cDLMV8p0
When visiting http://localhost:8080/ or http://localhost:8080/examples/something I only get 404's. Anyone has a clue how to get this working?

Try opening the application.properties file, look if there is a server.contextPath= variable.
Take this value and put it after your port value but before the resource value from your controller
e.g. :
http://localhost:8080/{yourContextPath}/something/?someRequestParam=someValue
Good luck

Related

How to override bootstrap.properties OR how to ignore cloud config for local environment in spring boot

I have an application [Spring boot app] which runs in local, dev env. I have used AWS parameter store configuration in bootstrap.properties file as below :
aws.paramstore.enabled = true
aws.paramstore.prefix = /abc
aws.paramstore.name = xyz/jkl/wer
These are my dependencies in pom.xml file :
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-starter-aws-parameter-store-config</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-sts</artifactId>
<version>1.12.263</version>
</dependency>
The app is running fine with the above configurations. However every time when I need to run my app in local, I need to comment out the above dependencies otherwise it throws error because these jars are in classpath and they complain about few connection not found etc.
I am trying to figure out some possibilities where I don't have to make any changes in the pom.xml for running the app with local-properties.
I tried to set the below property to false in bootstrap and enabled it in application-dev.properties file, however the app is not picking up the bootstrap configs in dev.
#false in bootstrap, enabled in application-dev.properties
aws.paramstore.enabled = false
Can you help me out to resolve this issue. I do not wish to comment out the dependencies in pom every time I run my app in local. What trick can I use here.
I have found one way to do that in eclipse :
However the main question remains valid though, how can we override bootstrap properties in our application environment wise.

Apache Ignite - how to enable Rest API when starting a node programmatically?

Rest API works for me once I copy Rest api lib to libs folder and starting from a command line (./bin/ignite.sh ./examples/config/example-cache.xml
) and then trying url commands (e.g. http://localhost:8080/ignite?cmd=version).
How to do that from a java code? I tried starting a node with same configuration as above but Rest api seems that is not enabled (url commands do not work).
Thanks.
Most probably when you start Ignite from java code you don't have rest api lib in your classpath.
If you use maven, add this to dependencies in pom.xml:
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-rest-http</artifactId>
<version>${ignite.version}</version>
</dependency>
Just don't forget to change ${ignite.version} to Ignite version you using.

Java - Error with Spring Integration Mock SftpServer

I am following this post about making a mock sftp server for tests with spring.
https://dzone.com/articles/spring-integration-mock-0. This consists of a setup of a mock sftp server, followed by putting a file there with a string as a content. The test is executed with the command:
$ mvn -Dtest=SftpRetrieveFileTest test
And the error I get is:
java.util.FormatFlagsConversionMismatchException: Conversion = s, Flags = #
By commenting blocks of code I determined that the error is caused when the file is put into the server, with these lines:
String uploadedFileName = "uploadFile";
sftpChannel.put(new ByteArrayInputStream(testFileContents.getBytes()), uploadedFileName);
I've already seen some workarounds that say that it may be a problem of the version of java (some said that it worked in 1.6 but not with 1.7, which I'm using). The complete project can be found and cloned from https://github.com/skprasadu/junit-testing-ftpflow-in-spring-integration.
Thank you.
I just pulled down the project and ran it under 1.6 and 1.8. The test runs fine under 1.6. It does not work under 1.8 and returns the same error you are seeing under 1.7. The workarounds are accurate. You should switch to using 1.6. It's a result of code using the String.fomat() method and # tag being passed to the format. There is actually a bug logged against the apache SSHD server project (RE: SSHD-104). This issue was addressed in version 0.6.0 of the project. So your other option is to change your maven dependency from
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>0.5.0</version>
</dependency>
to
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>0.6.0</version>
</dependency>
Then the project/test should work fine under JDK 1.7 as well.

Info: WebSocket connection closed, Code: 1006 while trying to run websocket samples

I've embedded tomcat 8.0.20 and using that in my server. I build the tomcat websocket example and when I'm trying to run it in my server for all 4 examples (Echo, chat, etc..)
I'm getting
Info: WebSocket connection closed, Code: 1006
In the console it prints something like (url is correct per sample)
WebSocket connection to
'ws://localhost:9763/examples/websocket/echoProgrammatic' failed:
Error during WebSocket handshake: Unexpected response code: 302
When I deploy this on tomcat it shows 404 error instead of 302.
Can someone tell me what could be the reason for this?
In the pom, my dependency is as follows
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
It seems that websocket implementations are not get deployed correctly. Can somebody help?
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
Use websocket API and websocket Implementation provided by Tomcat (groupId: org.apache.tomcat), instead of some random third-party jar.
You can either search Maven or look the pom files in the res/maven directory of the source thee.
Scope "provided" means that the jar is only used at compile time, not at run time.
BTW, current versions of Tomcat implement WebSocket 1.1.
Scanning for websocket endpoints is triggered by an implementation of javax.servlet.ServletContainerInitializer (as configured in META-INF/services/javax.servlet.ServletContainerInitializer file of tomcat-websocket.jar).
This was totally a mistake. In the tomcat example all the URLS are hard coded with examples but my context is example(without s). That was a mistake.

java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE from Mashape Unirest in Java application

I have a Maven Java project that uses Mashape Unirest for sending HTTP requests to other URLs. I am currently writing an integration test (using TestNG) that sends a normal HTTP request using Unirest. When I run the integration test through Maven (via the Failsafe plugin), the request is sent out successfully. However, when I try to run the integration test via Eclipse, I keep on getting the following error:
FAILED: getCurrentTimeTest
java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:52)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:56)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<clinit>(DefaultHttpRequestWriterFactory.java:46)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:72)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:84)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<clinit>(ManagedHttpClientConnectionFactory.java:59)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$InternalConnectionFactory.<init>(PoolingHttpClientConnectionManager.java:487)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:147)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:136)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:112)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:726)
at com.mashape.unirest.http.options.Options.refresh(Options.java:41)
at com.mashape.unirest.http.options.Options.<clinit>(Options.java:27)
at com.mashape.unirest.http.HttpClientHelper.prepareRequest(HttpClientHelper.java:141)
at com.mashape.unirest.http.HttpClientHelper.requestAsync(HttpClientHelper.java:80)
at com.mashape.unirest.request.BaseRequest.asStringAsync(BaseRequest.java:56)
at ...
I am also able to reproduce this error using a basic Java application script.
I have made sure that the dependencies I am using in my pom.xml file are the latest and greatest, as seen below:
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.2</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3.2</version>
</dependency>
I have also checked out the source code of BasicLineFormatter.java, both from the source file downloaded to Eclipse and from Apache's Httpcore Github repo. In the Github repo, notice how the INSTANCE field is defined for the 4.3.x branch and the trunk branch, but not in older branches like 4.2.x. However, I am indeed using version 4.3.2 in my project, so I should be using a JAR file for Httpcore that has the latest version of BasicLineFormatter. I know that, based on the Maven Dependencies JAR files that are in my project, that I am indeed using the latest versions of these Apache dependencies, not the older versions specified as downstream dependencies of my project.
I have checked other various SOF and blog posts about this issue, such as Mashape Unirest Java : java.lang.NoClassDefFoundError and this blog post too, but they all seem to be talking about solving the NoSuchFieldError problem for Android. However, I'm dealing with a standalone Java application, not an Android application.
I am at a loss in determining how to troubleshoot this issue. Anyone have any idea what I need to do?
UPDATE
Instead of showing my test case, I will reduce the illustration of a reproduction of this problem to just a simple one-liner Java application, because the problem exists with any Java application or test case run through Eclipse, not just one particular test:
System.out.println(Unirest.get("http://www.google.com").asStringAsync().get().getBody());
Normally, this should print the HTML of the Google home page, but I instead get the NoSuchFieldError stack trace.
FIXED!
The problem was that the AWS SDK (it's on my classpath because I'm developing for Elastic Beanstalk) had a conflicting JAR file. Using Oleg's solution (thanks BTW), I printed the following output in a unit test:
jar:file:/some/path/aws-java-sdk/1.7.1/third-party/httpcomponents-client-4.2.3/httpcore-4.2.jar!/org/apache/http/message/BasicLineFormatter.class
I'll have to rearrange my classpath so that AWS SDK is no longer conflicting.
The only plausible explanation to this problem is there is an older version of HttpCore on the classpath (unless you also want to consider a possibility of green men from Mars messing with your computer remotely from a flying saucer).
You can add this snippet to your code to find out what jar the class gets picked up from. This might help find out why that jar is on your classpath in the first place.
ClassLoader classLoader = MyClass.class.getClassLoader();
URL resource = classLoader.getResource("org/apache/http/message/BasicLineFormatter.class");
System.out.println(resource);
This basically tells me that in my case the jar resides in the local maven repository and likely to have been added to the classpath by Maven
jar:file:/home/oleg/.m2/repository/org/apache/httpcomponents/httpcore/4.3.1/httpcore-4.3.1.jar!/org/apache/http/message/BasicLineFormatter.class
As already mentioned by previous comments, It's mainly because of the conflicting versions of httpcore jar, the static field INSTANCE is been added to BasicLineFormatter class in versions > 4.3.1, Though you might have added the latest version of the httpcore jar in your dependencies, but its highly possible that other (lower) version of jar is getting picked up.
So, first to confirm that, wrong jar is getting picked up, Use the following line of code -
ClassLoader classLoader = <Your Class>.class.getClassLoader();
URL resource = classLoader.getResource("org/apache/http/message/BasicLineFormatter.class");
System.out.println(resource);
If this prints, the lower version of the jar, then it's confirmed that it's picking the lower version of the httpcore jar (May be from other dependencies of your project),
Solution -
Add following maven/gradle dependencies at the top of dependency list (Or above the other project dependency which caused the conflict) -
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.4.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4.1</version>
</dependency>
I faced the same exception using unirest:
java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:52)
at com.mashape.unirest.http.options.Options.refresh(Options.java:55)
at com.mashape.unirest.http.options.Options.<clinit>(Options.java:36)
And found it was due to DefaultConnectionKeepAliveStrategy.INSTANCE; and the conflicting jar was apache-httpcomponents-httpclient.jar in my classpath. Adding this post to help anyone who faces similar exception
I got this Exception: Caused by: java.lang.NoSuchFieldError: INSTANCE
Solution:
This happens if you have two different version classes in your classpath…. […], So I first find that class (one version of class), click that class, select build path, then I click remove from build path.
if you are using aws sdk this error occurs because of dependency mismatch.
To avoid this error do the following:
1.Put the dependecies in the required order aws sdk and the end preferably
2.Add shade plugin to the project
This solved my problem
you can refer to my answer in
HTTPClient Example - Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
my case is i have httpclient-4.4.1.jar, and httpcore-4.4.1.jar in my class path, but JVM loaded BasicLineFormatter from httpcore-4.0.jar

Categories

Resources