spring tool suite can not deploy example webservice on tomcat - spring

I have some experience with EJB and JBoss and know the basics about Webservices, but I'm new to Spring.
So I tried to deploy the example Spring WS project gs-rest-service-complete without any changes. It is running on Spring Boot, but I cannot deploy and access it on an external Tomcat Server.
This is what I did: I installed Spring Tool Suite Version: 3.6.4.RELEASE and
Apache Tomcat 8.0.24 and defined Tomcat as a new Server in Spring Tool Suite.
It seems to work because I can deploy (and access) the example Spring MVC project and I can deploy another (not Spring Example) Webservice on Tomcat.
However I cannot deploy the gs-rest-service-complete project. I changed packaging in pom.xml to 'war', but it didn't help. Any hints what I could do?
Thanks, caduta

Finally I found the answer at the bottom of this site:
https://spring.io/blog/2014/03/07/deploying-spring-boot-applications
I had to do three steps to get it running:
change packaging to war in pom.xml.
comment out the declaration of the spring-boot-maven-plugin in pom.xml.
change the Application class to inherit from SpringBootServletInitializer and override method configure. This is necessary to register the application in tomcat.
Now the Application class looks like this:
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
#Configuration
#ComponentScan
#EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
private static Class<Application> applicationClass = Application.class;
public static void main(String[] args) {
SpringApplication.run(applicationClass, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
}

Related

spring boot application deployed as war on weblogic- not working

i am new to weblogic, struggling to deploy war. I have a spring boot application running on embedded tomcat.I changed main class as follows.
#SpringBootApplication
#Configuration
#ComponentScan("com.fmc.*")
#EnableAutoConfiguration
public class ApplicationBoot extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(
ApplicationBoot.class, args);
}
#Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return application.sources(ApplicationBoot.class);
}
}
i changed packaging as war and excluded the tomcat jars and deployed. But when a submit request, I am getting 404.
I don't see anything in logs in the diagnostics in admin console. Is there aythere place to see the logs. In the weblogic admin console,I can see the application as active and health as ok.
I changed the same application spring web application, it's running successfully, but not spring boot application.
Thanks for your help.
public class ApplicationBoot extends SpringBootServletInitializer implements WebApplicationInitializer
main class has to implement WebApplicationInitializer, thought of useful for some one

Spring Boot MongoRepository unit testing using pre-installed MongoDB

I have a regular Spring Boot application (1.3.2) with MongoDB using MongoRepository.
I would like to write an integration test for one of my endpoints that gets the data from the MongoDB. As far as I see from the Spring Boot 1.3 Release Notes Spring has auto-configuration for Embedded MongoDB (de.flapdoodle.embed.mongo). However, I cannot figure out from Spring and flapdoodle documentation on how to write an integration test that would use already installed version of MongoDB on my filesystem.
So far my integration test looks like this:
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(Application.class) // my application class
#WebAppConfiguration
public class IntegrationTest {
#Autowired
private MyRepository myRepository;
#Before
public void setup() {
myRepository.save(new MyEntity());
}
#Test
public void test() {
// here I will fire requests against the endpoint
}
}
I have added two dependencies with test scope: spring-boot-starter-test and de.flapdoodle.embed:de.flapdoodle.embed.mongo. So when I run the test, I can see that flapdoodle attempts to download version of MongoDB, but fails since I am behind the proxy. But I do not want to download any versions, I want it to use my locally installed MongoDB. Is it possible to do this?
If you want to use your locally installed MongoDB (not recommended, since then the tests depend on a particular DB that can get into a dirty state), then you shouldn't be pulling in the embedded MongoDB.
I believe this config will do what you're asking for, though (seems to work in my Spring Boot 1.3.5 test):
import java.net.UnknownHostException;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import com.mongodb.MongoClient;
#EnableAutoConfiguration(exclude = MongoAutoConfiguration.class)
#Configuration
public class TestConfig
{
#Primary
#Bean
MongoClient mongoClient()
{
try
{
return new MongoClient("localhost", 27017);
}
catch (UnknownHostException e)
{
throw new RuntimeException(e);
}
}
}
However, I suspect you would be better off properly configuring the proxy and using the embedded mongoDB in your tests. See this answer for hints on how to do that.

Spring Boot application war in a standalone servlet container

A general question about building a war from a spring boot application and running it in a standalone servlet container. The documentation I've seems seems at odds with examples on Stack Overflow.
The answer here shows the way I read of doing this a couple of months ago. I read this here, but the guide seems to have changed losing the actual example app.
Here the "configure" method references the main spring boot Application.class.
public class WebInitializer extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
There are also these posts here and here that show the "configure" method referring to the SpringBootServletInitializer sub class itself.
public class BootStrap extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(BootStrap.class, args);
}
#Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return application.sources(BootStrap.class);
}
}
and also there is a main method.
Also the spring-boot-sample-traditional  example app at https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples, which shows "WAR packaging"
does it differently
public class WebConfig extends WebMvcConfigurerAdapter {.........
I was wondering is there are issues with choosing over these different ways of seemingly achieving the same thing in spring boot? Or do they all work equally as well and are interchangeable?
Having your main application class extend SpringBootServletInitializer (Bootstrap in your question) or using a separate class (WebInitializer in your question) is down to personal taste. My preference is to take the Bootstrap approach but they both work in the same way; pick which ever you prefer.
If you are only going to deploy your application to a standalone servlet container then you don't need a main method. The main method is used if you want to run the application as an executable war (java -jar my-app.war), or you want to be able to run it directly in your IDE, i.e. without having your IDE deploy it to a servlet container.
spring-boot-sample-traditional illustrates the use of web.xml to Bootstrap a Spring Boot application. Generally speaking, this isn't a recommended approach unless you're stuck on a Servlet 2.5 container. The use of WebMvcConfigurerAdapter has nothing to do with WAR packaging. Take a look at its web.xml to see the relevant pieces of configuration.
Use Spring Initializr
http://start.spring.io/
Choose your project type (Gradle or Maven) and Packaging as war.
Add Web as dependency and Generate the project.
This will bootstrap your app with the "correct" way.

Spring Boot on Elastic Beanstalk worker tier

I am trying to deploy a spring boot app into one EB worker tier but seems that EB it is not ready to manage this kind of project.
Have I to mandatory generate a .war from my spring boot app?
Thanks!
I have found the problem.
EB expects a .war file and Spring Boot app usually is launche by a embedded Tomcat or Jetty.
I have found the solution in this guide:
http://spring.io/guides/gs/convert-jar-to-war/
Summing up:
Add tomcat dependency with provided scope in pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Create a class extending SpringBootServletInitializer and load the entrypoint within this class. This way, we are indicating to the servlet container how to launch the app.
package com.proyecti.magma.conversionsworker.config.servlet;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import com.proyecti.magma.conversionsworker.entrypoint.Application;
public class ServletConfig extends SpringBootServletInitializer
{
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}

How to configure JMX with Spring Boot

I have created a Spring Integration application with Spring Boot. I would like to know how to configure JMX with Spring Boot. I believe by default JMX is configured when using Spring Boot Actuator.
Do I need to configure anything else to be able to export MBeans for Spring Integration?
Most of the example I see have the following line in the applicationContext.xml
<context:mbean-export/>
<context:mbean-server/>
My Application.java class looks like this.
package com.jbhunt.app.consumerappointmentintegration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
#Configuration
#ComponentScan
#EnableAutoConfiguration
#ImportResource("classpath:META-INF/spring/integration/spring-integration-context.xml")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Adding this line to the configuration doesn't seem to export the Spring Integration mbeans
#EnableIntegrationMBeanExport(server = "mbeanServer", defaultDomain="my.company.domain")
I'm referencing this video https://www.youtube.com/watch?v=TetfR7ULnA8
As you understand the Spring Integration JMX is enabled by default, if you just have spring-integration-jmx in the classpath. And, of course, if spring.jmx.enabled = true (default).
You can't overrride that just declaring one more #EnableIntegrationMBeanExport, because it is based on #Import and you just can't override import classes because of (from ConfigurationClassParser):
imports.addAll(sourceClass.getAnnotationAttributes(Import.class.getName(), "value"));
If imported classes are already there, they aren't overridable.
You have several choices to achieve your requirements:
Disable default Spring Boot JMX - just addind to the application.properties spring.jmx.enabled = false and continue to use #EnableIntegrationMBeanExport
Configure IntegrationMBeanExporter #Bean manually.
Just configure your my.company.domain domain in the application.properties:
spring.jmx.default_domain = my.company.domain
It is quite late to add this; but in addition to the endpoints.jmx.domain I found it useful to change the spring.jmx.default-domain to someting unique per application
This is with multiple instances of Spring Boot 1.4.1 apps running in Tomcat 7

Resources