access resource files in outer jar - java

I'm writing some microservices (fat jar) which expose some http endpoint using a router. As I like every microservice automatically expose a /doc endpoint which just fetches a /src/main/resource/doc/readme.adoc and renders that to html I created a new project doc-renderer.
It puts a /doc endpoint to the router and in its handler method it tries to access the readme.adoc and renders it to html. If I now use that bundle as dependency within my microservices and put a readme.adoc under /src/main/resources/doc/ it isn't really working as it loads the readme.adoc in the dependend bundle.
So this is the resulting jar structure
microservice.jar
- /doc/
- readme.adoc
- /lib/
- docRenderer.jar ( here it tries to load the readme from outside )
- /doc/
- readme.adoc
To load the resource I use: getClass().getResourceAsStream("/doc/readme.adoc")
Is there a way I can force it to always use the /doc/readme.adoc file from the outer jar?
Regards, Marco

Related

Spring Boot with devtools call SOAP API

I am deploy an project Spring Boot, using devtools(spring-boot-devtools) and call a Soap service.
I generate the Soap class into /src/main/resources/templates/generated
and add this folder as Source Code.
Because when call this Soap service, its have a problem:
java.lang.IllegalArgumentException: ...ClassV11PortType referenced from a method is not visible from class loader
So, I was add the spring-devtools.properties file to /src/main/resources/META-INF/spring-devtools.properties
and add this line to spring-devtools.properties file:
restart.exclude.mygeneratedclasses=/[packageOfGeneratedClass].class
Then now, I can call the SOAP service successful.
But now, my project cannot reload automatically when i modified some code.
I was try to edit some code anywhere and save but not luck, my project doesnot reload.
Instead of excluding generated files, you can try to include JAR responsible for loading these classes into restart classloader (used in spring-devtools).
For dependency com.sun.xml.ws:jaxws-rt:2.3.2-1, update /src/main/resources/META-INF/spring-devtools.properties like this:
restart.include.jax=/jaxws-rt.*\.jar
Github issue reference: Devtools cannot be use with jaxws-ri #19379

Java MVC project in Spring Tool Suite additional files

I'm learning to make Java MVC project using Spring Tool Suite tool.
The path to make new project is:
File->New->SpringLegacyProject->Spring MVC Project.
My question is: which directory I have to use to add additional not-Spring files and where and what do I have to type for Spring files to see them?
For example:
css files - where to put and how to make jsp views see them, will 'link rel="" 'tag be enough?
properties files used to specify database connection or to specify messages for ReloadableResourceBundleMessageSource. In this case, do I have to create bean for this class in root-context.xml?
Thanks.
You should probably use Spring Boot (i.e. use File->New->Spring Starter Project and select Web as a starter. Place your web resources under src/main/resources/static folder. They are picked up automatically from that folder.
You should try an example project: File -> New -> Import Spring Getting Started Content and then pick "Serving Web Content" from the list.
Try some DB getting started content example to get the answer for the second part of your question.

Configure Swagger UI (Jersey REST API with Maven/Tomcat)

I have found similar Stack Overflow posts for configuring Swagger + SwaggerUI + Jersey, but almost all of these use Spring and I have not been able to find a solution for what I am trying to do.
I have Swagger + Jersey working, and have copied the swagger-ui/dist folder from: https://github.com/swagger-api/swagger-ui to my webapp directory, and have edited swagger-ui/dist/index.html file to point to my swagger.json output. I can properly access Swagger UI at http://localhost:{port}/{base-path}/dist/index.html
However, I do not want to have to copy the pre-build dist files to my webapp directory every time I need to fetch updates. I would like to automate this with Maven (which I am new to as well as Jersey/Swagger), and found this dependency online: https://mvnrepository.com/artifact/org.webjars/swagger-ui/2.0.12
However, I do not understand how to configure the path from my webserver to this webjar dependency.
To recap, I have Swagger UI working on my local machine, but only by manually copying the pre-build files to my webapp directory and would like some way to automate this (I have found solutions using Spring, but I am not using this).

Spring boot serving static resource

I work on spring boot application. I'm trying to serve static content with spring.
want to serve a resource stored in the /c:/frontend/files/ directory whenever a request comes in for the URL matching the pattern: /file/**:
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/file/**")
.addResourceLocations("file:///C:/frontend/files/" );
}
but when i try to access to this resource using this url: http://localhost:9999/file/app.min.js
I have this problem
There was an unexpected error (type=Not Acceptable, status=406).
Could not find acceptable representation
I resolved the problem. it's related to "spring-cloud-config-server". I just delete this config: org.springframework.cloud spring-cloud-config-server
It sounds like your project's folder structure is wrong.
Code should go under src/main/java and resources (like your javascript) should go under src/main/resources. You have a few different options where you can actually serve the files from. This post on the spring.io blog has the following to say:
Spring Boot will automatically add static web resources located within any of the following directories:
/META-INF/resources/
/resources/
/static/
/public/
Another option you also have is using webjars.
Personally, I've found it easiest to put those kind of files under src/main/resources/public. It always works without any issues for me. The interesting thing is you can put a folder named /public anywhere in your project and spring-boot will serve files out of it. You have to be really careful that it's under src/main/resources/public though if you're using a build tool like maven, as when you come to build your .jar the files won't be in the right place otherwise.

exception":"java.lang.ClassCastException in liferay service builder

I have worked on implementation of the Entity remote service.
I have created one custom service method in EntityServiceImpl,Created custom service method providing service through InstitutionServiceUtil.
After deploy the portlet, while sending request to service method through from browser window,I am getting below Exception
exception":"java.lang.ClassCastException: com.institutions.model.impl.InstitutionImpl
cannot be cast to com.institutions.model.Institution
Note: If I send the request after restart the server, I didn't get above exception.
How to resolve the above Exception?
I assuming that InstitutionImpl implements the interface Institution. If so, then the root cause of the exception is classloading: Classloader A did load Institution but InstitutionImpl was loaded from a different classloader. Two classes in Java are only equivalent if the fully qualified name and the classloader are the same.
I don't know enough about liferay to tell you how it's class loading works. But to solve the problem, you need to find out if the Institution interface could already be around when you try to load your implementation (maybe from a previous deployment attempt).
While deploying the portlets that throws the class cast exception, do the following:
deploy the application in the liferay/deploy.
shutdown the liferay
move the service jar from the WEB-INF/lib from the portlet to the /lib/ext of the tomcat
remove the temp and work folder from the tomcat
restart the tomcat.
OR ...what worked for me was
change the package name while building the service.xml in the service.xml file
Or if you have already built the service, do these steps
Just delete the 5 packages that are created from the service builder,
i.e
model.impl
service.base
service.http
service.impl
service.persistence
delete the .xml generated in the META-INF folder except for the file ext-spring.xml
delete the XX-service.jar from the docroot/lib folder
delete the service folder in the docroot folder.
change the package name in the service.xml and build the path.

Categories