Spark rest api server deploy - java

With this link, I made rest api server. It works well when I run on eclipse ide. However, I don't know how to deploy on server. I made war file and tried to deploy on tomcat, but somehow cannot access any page that I defined, unlike running on eclipse. Here is some gradle configuration that I made.
apply plugin: 'war'
war {
baseName = 'server'
manifest {
attributes 'Implementation-Title': 'SparkAPIServer', 'Implementation-Version': '1.0', 'Main-Class': 'com.server.Main'
}
}
I'm sure that 'Main-Class' path is correct. Any idea of this?

Running from Eclipse IDE is not the same like running on Tomcat, because when running on Eclipse Spark uses the built-in Jetty server, and when deployed to Tomcat Spark runs on Tomcat server.
Quoting from the documentation:
Other web server
To run Spark on a web server (instead of the embedded jetty server),
an implementation of the interface spark.servlet.SparkApplication is
needed. You have to initialize the routes in the init() method, and
the following filter has to be configured in your web.xml:
...
So in order to run the same code after deployed to Tomcat, you need to:
Your class should implement SparkApplication.
Implement the init() method, and register all your routes there. (Of course, if you want to be able to run both locally on Eclipse and remotely on Tomcat, just register all your routes to some private method startSpark() which will be called both from init() and from main()).
Your web.xml should be set accordingly.

Seeing that you use Spark and a main method to start a webserver, you are looking actually to create a jar, like the following (adapt as needed):
jar {
manifest {
attributes 'Main-Class': 'com.foo.bar.MainClass'
}
}
See the jar task documentation. And don't forget to add the classpath for your external libraries into the manifest.

Related

Tomcat does not unpack spring boot application

thank you for your attention in reading my problem;
I'm trying to deploy a spring boot application but when I put it in the tomcat webapps folder it doesn't load my application, does anyone know what could be happening?
I already tried to run several versions of java changing the system variables, I used several versions of tomcat but none unzip the application
I'm using java version 17
and version apache-tomcat-9.0.71
Log Tomcat
Tomcat Past
I thought Spring Boot came with a pre-installed web server, so there is no need to use this method. Why don't you use the current method which supports auto deployment of your application?
An Spring Boot application is a ready to run jar file which contains tomcat/jetty and can be run with java -jar <your app>.jar.
When you want to run an application in tomcat you should build a .war file.
======================================
As found here: https://stackoverflow.com/a/27905557/2144466
Did you follow this guide: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto.traditional-deployment
and do you have a class which extends SpringBootServletInitializer andd overwrites the configure method:
`
#SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
`
Please follow below.
Rename your war file at location webapps from web-0.0.1-SNAPSHOT.war to web.war
restart tomcat.
open url (http://localhost:8080/web) in browser.
I managed to solve the problem.
Apparently it was version incompatibility between spring, tomcat and java, by adjusting the system variables I can deploy both java 11 and java 17 applications.
Even though I managed to find a solution, I thank the friends above for dedicating their time to trying to solve my problem.

Using SpringExtension with Tomcat and Hibernate web app

We are trying to use spring-test's SpringExtension to write integration tests for our Spring and Hibernate-based Tomcat web application. Our sessionFactory bean configuration has the property configured mappingJarLocations with a sample value as /WEB-INF/lib/company-common*.jar which contains hibernate mapping files. In both actual deployment and Eclipse dev deployment, this works fine as the docBasePath (in Servlet environment) is appended to this pattern and the files are getting resolved. But this is not the case while running JUnit test cases either in a local or a CI environment.
We tried our best to use the provided support by having few overridden implementations of WebTestContextBootstraper, GenricXmlWebContextLoader, XmlWebApplicationContext, and WebDelegatingSmartContextLoader but had to finally give up as we cannot override the final method org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(MergedContextConfiguration) to provide the custom implementation of XmlWebApplicationContext. Our current approach is to manually create the application context and use it in the tests.
Here is the project structure:
Project_WebApp
|--src/**
|--WebContent/**
|--pom.xml
When the app is packaged as Project_WebApp.war, the dependencies are inside WEB-INF/lib from the root of extracted war. When deployed as a webapp in Tomcat using Eclipse, the dependencies are copied to <Eclipse_Workspace_Dir>/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/Project_WebApp/WEB-INF/lib. In both cases, the dependencies are available at <Resource_Base_Path>/WEB-INF/lib and Resource_Base_Path has no relation to Project_WebApp base directory.
Questions:
Did any one use SpringExtension in a scenario similar to above? If so can you suggest any alternative approaches?
Instead of /WEB-INF/lib/company-common*.jar, we tried a classpath-based pattern but didn't work as the obtained class path resources don't match the pattern. Is there anything else to try here?

How to run a java task automatically from an external jar?

I want to generate a java jar which when included on the classpath of another project will launch a periodic task that does something in the background.
This is very similar to eureka client. You include the dependency and add an annotation after which a service is started automatically to poll eureka server.
How can I do that?
Edit: I got it to work using maven, following the example provided in the comments
github.com/shauank/spring-boot/tree/master/client (client which is having taskexecutor)
github.com/shauank/spring-boot/tree/master/application (Application which uses jar created in step1)
You can use concept of Autoconfiguration. Same is used by Eureka and Config server.
Under src/main/resource create spring.factories and add following entry
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
location.to.your.executor
Your class,
pacakage location.to.your.executor
class MyExecutor{
public MyExecutor(){
//Your code for task executor
}
}
Now, above code can be build as jar and included into another spring boot project.
So, when you run another jar, spring boot will look for auto configuration onto spring.factories class and load classes defined into it.

Package JBoss AOP interceptors with business dependencies as a standalone application

Here is my needs :
I have a WebService application running on a JBoss 5.1 (with a jboss-classloading configuration export-all=NON_EMPTY and import-all=true).
I want to add some interceptors on it but I can't change the legacy WS WAR.
What is my problems :
I managed to get working a simple interceptor packaged in a JAR that basically log every calls on legacy WebService.
But to achieve my goal, I need to use JAR dependencies like XML parsers, business objects, etc...
How can I package my AOP interceptors and its dependencies in one file ?
What I've tried :
In a WAR package, I have to use a similar jboss-classloading strategy to avoid conflicts, but in this context, I can't intercept anything else than classes in the WAR itself, so I can't intercept legacy WS calls.
PS : I have made all the basics to get JBoss AOP working properly :
pluggable-instrument.jar in bin folder
run.conf with -javaagent:pluggable-instrumentor.jar
aop.xml with loadTimeWeaving enabled and include package defined
jboss-aop.xml with valid pointcuts configurations (which I want in my package)
For now I found a solution by building a SAR package (JBoss Service Archive).
The SAR file look like this :
aop-interceptors.sar
META-INF/jboss-service.xml
aop-interceptors.jar
META-INF/jboss-aop.xml
com.mypackage.aop.interceptors...
com.mypackage.aop.handlers...
com.mypackage.aop.business.logic...
xml-parsers.jar
business-objects.jar
...
The SAR file is deployed in folder : jboss-5.1/server/default/deployers/jboss-aop-jboss5.deployer/
I don't know what SAR package is supposed to do ... is there a better way to do it ?

web client for web service

I've a web-service that works fine when I access them from a J2SE (desktop) application. To access this service I do follow:
generate stub classes by wsdl link using java wsimport tool
then I create service using generated classes and run one of wsdl operations.It looks like this:
MyWebServiceService webService = new MyWebServiceService();
MyWebService port = webService.getMyWebServicePort();
webService.run("XYZ");
As I sad it work fine when I use it in a standalone application.
But...when I try to access web-service in the same way but from servlet-client, using generated stubs I get following error:
java.lang.ClassCastException: com.sun.xml.bind.v2.runtime.JAXBContextImpl cannot be cast to com.sun.xml.bind.api.JAXBRIContext
org.jboss.ws.metadata.umdm.EndpointMetaData.eagerInitializeAccessors(EndpointMetaData.java:686)
org.jboss.ws.metadata.umdm.EndpointMetaData.initializeInternal(EndpointMetaData.java:567)
org.jboss.ws.metadata.umdm.EndpointMetaData.eagerInitialize(EndpointMetaData.java:553)
org.jboss.ws.metadata.builder.jaxws.JAXWSClientMetaDataBuilder.rebuildEndpointMetaData(JAXWSClientMetaDataBuilder.java:314)
org.jboss.ws.core.jaxws.spi.ServiceDelegateImpl.getPortInternal(ServiceDelegateImpl.java:271)
org.jboss.ws.core.jaxws.spi.ServiceDelegateImpl.getPort(ServiceDelegateImpl.java:202)
javax.xml.ws.Service.getPort(Service.java:143...
I've searched google long time but found nothing helpful topics. Some topics show examples accessing web-services from servlet, but unfortunately I can't do this...( And don't know what is cause of trouble.
Application server: jboss 4.2.3GA
Is it possible to connect web-service from servlet? How?
I've tried use #WebServiceRef annotation, but it seem web-container can't inject web-service stub. And I think that container must not do this itself, because stub classes have already been generated by wsimport tool, and its enouph to use this classes for accessing of web-service.
Stub classes were generated using the following command:
wsimport -keep -p com.myhost.ws http://www.myhost.com/services/MyWebService?wsdl
Did you make sure your classpath does not contain multiple JAX-B Jars with differing versions ? The exception looks like a version conflict to me. Application servers usually have some kind of "endorsed" lib directory that holds JARS that are always added in front of web application classpaths. Maybe your app server has a conflicting JAX-B implementation there ?
If you use Maven to package your application, make sure transitive dependencies don't pull in unwanted JAX-B Jars (use 'mvn dependency:tree' to check this).
This definitely sounds like a JAXB conflict to me. Check out the jaxb versions that you have in your war and make sure that they are not conflicting with a jaxb jar that Jboss may have in its lib directory.
Addytionally if jbossws-native library was installed correctly the following packages should be deleted from jboss_home/lib/endorsed directory:
jboss-jaxrpc.jar
jboss-jaxws-ext.jar
jboss-jaxws.jar
jboss-saaj.jar
Otherwise you don't have ability to connect to web service through EJB or servlet.

Categories

Resources