Spring boot addInterceptors not being called in Websphere application server - java

I have a Spring Boot app using SpringWS. Inside of the WsConfigurerAdapter I am overriding addInterceptors in order to add logging/authentication/validation/etc.
#Configuration
#EnableCaching
#EnableWs
public class WebServiceConfig extends WsConfigurerAdapter {
...
#Override
public void addInterceptors(List<EndpointInterceptor> interceptors) {
EnvironmentSettings environment = getEnvironmentSettings();
interceptors.add(getLogSetupInterceptor());
interceptors.add(getAuthenticationInterceptor());
interceptors.add(getServerLoggingInterceptor());
interceptors.add(getAuthorizationInterceptor());
ServerPayloadValidatingInterceptor validatingInterceptor = new ServerPayloadValidatingInterceptor();
validatingInterceptor.setValidateRequest(environment.isValidateSoapRequest());
validatingInterceptor.setValidateResponse(environment.isValidateSoapResponse());
validatingInterceptor.setXsdSchema( xsdSchema());
interceptors.add(validatingInterceptor);
}
}
What is strange is that when run locally, all of these interceptors are being added and run for every request just fine. However, when I deploy the application as a .war to WAS, this one method is not being run. I even added logging statements and I can tell that it's this method that's getting skipped over instead of the interceptors themselves. Does anybody know something about spring boot .war files that I don't?
Also, here is my Application class:
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

I figured out what I did wrong. It turns out I was creating a servlet mapping in both my WebServiceConfig and my web.xml both at /*.

Related

How to deploy a Spring-Boot-Webflux application to Tomcat standalone server?

A normal spring-web application can be deployed to tomcat standalone as war file as follows:
#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);
}
}
Question: how can I deploy such an application after migrating to spring-webflux to tomcat?
Docs say:
https://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html#webflux-httphandler
To deploy as a WAR to any Servlet 3.1+ container, you can extend and
include AbstractReactiveWebInitializer in the WAR. That class wraps an
HttpHandler with ServletHttpHandlerAdapter and registers that as a
Servlet.
So but there is no example how to.
I tried as follows, which gives an exception:
#SpringBootApplication
public class MyApplication extends AbstractReactiveWebInitializer {
#Override
protected Class<?>[] getConfigClasses() {
return new Class[] {MyApplication.class};
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Result:
MyApplication.java:13:8
java: cannot access javax.servlet.ServletException
class file for javax.servlet.ServletException not found
This use case is not supported by the Spring Boot team, as explained in the reference documentation. Even if some features might work, you'll find many limitations and bugs to this approach - and it seems you've started to experience just this.

Spring Boot can connect to Cassandra when running as Servlet but not as Command Line

Background: I'm trying to set up a code-based data migration system for our Cassandra database. I don't have a ton of experience with Java, but if this were a .NET project I'd set up the migrations as a different project under the same solution. However, based on guidance from other team members that are more experienced, it was recommended that I include the migrations in the same package as the rest of the application (which I'm fine with). It was also suggested that the easiest method would be to run the migrations via a web API endpoint (which I'm more skeptical of). In the interest of avoiding opening up a potential security vulnerability, I thought I'd take a shot at making a command-line utility to execute the migrations.
I have a Spring Boot web application with an entry point class that looks like this:
#Configuration
#SpringBootApplication
#EnableAutoConfiguration
#EnableCaching
#EnableScheduling
public class MyApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
public static void main(String[] args) {
new MyApplication().configure(new SpringApplicationBuilder(MyApplication.class)).run(args);
}
}
However, I'm trying to add the functionality to run a couple migration scripts that are packaged with this application via the command line (e.g. java -jar MyApplication.jar migrate), so I added the following class:
#Configuration
#SpringBootApplication
#EnableAutoConfiguration
public class MigrationRunner implements CommandLineRunner {
#Autowired
Session session;
#Override
public void run(String[] args)
{
MigrationResources mr = new MigrationResources();
mr.addMigration(...);
mr.addMigration(...);
MigrationEngine.withSession(session).migrate(mr);
}
}
And then updated my entry point class like this:
// annotations
public class MyApplication extends SpringBootServletInitializer {
private final static String MIGRATE_COMMAND = "migrate";
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
public static void main(String[] args) {
if (args.length > 0 && args[0].equalsIgnoreCase(MIGRATE_COMMAND)) {
new SpringApplicationBuilder()
.sources(MigrationRunner.class)
.run(Arrays.copyOfRange(args, 1, args.length));
} else {
new MyApplication().configure(new SpringApplicationBuilder(MyApplication.class)).run(args);
}
}
}
The problem is that when I execute this with the migrate arg, Spring throws this error:
Error creating bean with name 'migrationRunner': Unsatisfied dependency expressed through field 'session'
Error creating bean with name 'session' defined in class path resource [org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.class]: Invocation of init method failed
All host(s) tried for query failed (tried: server022/XX.YY.ZZ.022:9042 (com.datastax.driver.core.exceptions.TransportException: [server022/XX.YY.ZZ.022:9042] Connection has been closed), server022/XX.YY.ZZ.020:9042 (com.datastax.driver.core.exceptions.TransportException: [server020/XX.YY.ZZ.020:9042] Connection has been closed), server020/XX.YY.ZZ.021:9042 (com.datastax.driver.core.exceptions.TransportException: [server020/XX.YY.ZZ.021:9042] Connection has been closed))
Running it without the migrate arg still works fine. I suspect that Spring is simply not picking up the correct certificates for this Cassandra server, even though it appears to be getting all the other configuration properties (server name, keyspace, etc.)
Question: How can I make a Spring Boot servlet that also has a command-line mode and can connect to the configured Cassandra server in both modes?
All you need to do is,
#SpringBootApplication
public class MyApplication
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
You have over complicated the application. If you run the MyApplication.main that will run in port 8080 by default.
Bonus, If you need both to start from same class.
#SpringBootApplication
public class MigrationRunner implements CommandLineRunner {
#Autowired
Session session;
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
#Override
public void run(String[] args)
{
MigrationResources mr = new MigrationResources();
mr.addMigration(...);
mr.addMigration(...);
MigrationEngine.withSession(session).migrate(mr);
}
}

Is "main" method of spring boot application required when deploy as war

When we deploy the spring boot application as a "war" on tomcat, we have to extend SpringBootServletInitializer and override the “configure” method.
When I comment "main" method, it still works and spring boot application and my services work fine. Do we still need main method when deploy project as "war" and why?
#SpringBootApplication
#EnableWs
public class TestApp extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder
builder) {
return builder.sources(TestApp.class);
}
public static void main(String[] args) {
SpringApplication.run(TestApp.class, args);
}
}
#SpringBootApplication
#EnableWs
public class TestApp extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder
builder) {
return builder.sources(TestApp.class);
}
/*public static void main(String[] args) {
SpringApplication.run(TestApp.class, args);
}*/
}
No you don't because nobody will ever call it.
Expect for the case if you want to run it outside of the web container.

Spring Boot version update breaks repository

See edit at the end...
I updated the dependencies of an app from 1.5.3-RELEASE to 1.5.9-RELEASE
***************************
APPLICATION FAILED TO START
***************************
Description:
Field mrepo in com.test.svc.TestHandler required a bean of type 'com.test.data.MongoTestRepository' that could not be found.
Action:
Consider defining a bean of type 'com.test.MongoTestRepository' in your configuration.
My Application.java is at com.test.Application and has the usual stuff:
#ServletComponentScan
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
My TestHandler is a #Service
#Service
public class TestHandler {
#Autowired
private MongoTestRepository mrepo;
...
}
My MongoTestRepository is a #Repository interface that operates on MongoTest classes
#Repository
public interface MongoTestRepository extends MongoRepository<MongoTest, String> {
}
So, with the older version of the spring-boot packages (1.5.3-RELEASE) everything worked fine. Updating to 1.5.9-RELEASE and it builds but gives me the above error on deployment/startup.
What's going on here? Is there some new annotation that I'm missing? Why would a minor version update suddenly break this?
EDIT=I am wrong, it's an update to the spring-data-mongodb package that broke it. It was updated from 1.10.9-RELEASE To 2.0.2-RELEASE at the same time and I didn't notice right away.

No mapping found on Spring-Boot

I'm new a spring-boot and spring framework. According to me, web app create and deploy very easy with spring-boot but when i run my sample spring web app, application not found "welcome.html" page. I checked all similar question on stackoverflow and not worked me. I cannot see little issue but I didnt find my problem. My application structure and codes are below:
MyApplication class is below:
#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);
}
}
WelcomeController class is below:
#Controller
public class WelcomeController {
#RequestMapping(value = "/welcome")
public String welcome() {
return "welcome"; //<- this is your login.html if it is directly in resource/templates folder
}
}
application.properties file is below:
spring.mvc.view.prefix = templates/
spring.mvc.view.suffix = .html
spring.mvc.static-path-pattern=/resources/**
WebMvcAppConfig class is below:
public class WebMvcAppConfig extends WebMvcConfigurerAdapter {
#Override
public void addViewControllers(ViewControllerRegistry registry) {
super.addViewControllers(registry); //To change body of generated methods, choose Tools | Templates.
registry.addViewController("/welcome").setViewName("welcome.html");
}
}
Firstly thanks a lot for quickly response my question #Andy Wilkinson and georges van. I looked for in spring boot reference guide and Serving Web Content with Spring MVC and I learned a lot of information about spring-boot. I removed WebMvcAppConfig because this class not necessary for starter and removed SpringBootServletInitializer. I moved html files into templates as you say. I keep simple and application run without issues.

Categories