Debug Spring Boot application in VSC - spring-boot

I have one simple Spring boot app that runs normally in console.
But I can't debug it in VSC.
The code is:
package com.sample.service.sample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
/**
* sample service
*
*/
#EnableDiscoveryClient
#SpringBootApplication
#EnableBinding(Source.class)
#EnableMongoRepositories(basePackages = "com.sample.service.sample.repositories")
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
}
The exception is:
nested exception is java.lang.NoClassDefFoundError: org/objenesis/strategy/InstantiatorStrategy
What did I miss in VSC setting for Spring Boot?

Related

Error creating bean with name 'primeLocateCometDService' when spring boot 1.5.19 with CometD 3.0.9 and tomcat 8.5.37

I just upgrade a tomcat project to spring boot project with CometD. Here are two services classes named: PrimeLocateCometDService & AbstractRealtimeCometDPublishService
When I start the project, error occurred.
public AbstractRealtimeCometDPublishService(BayeuxServer bayeuxServer, String sessionName) {
this.bayeuxServer = bayeuxServer;
this.localSession = bayeuxServer.newLocalSession(sessionName);
this.localSession.handshake();
}
Handshake will throw nullpointerexception.
package com.citi.pf.realtime;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
import org.cometd.annotation.AnnotationCometDServlet;
import org.cometd.bayeux.server.BayeuxServer;
import org.cometd.server.BayeuxServerImpl;
import org.cometd.server.transport.JSONPTransport;
import org.cometd.server.transport.JSONTransport;
import org.cometd.websocket.server.WebSocketTransport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration;
import org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.ImportResource;
import com.citi.pf.portal.lib.util.ENVUtils;
#SpringBootApplication
#ImportResource({
//webapp
"classpath:META-INF/realtime/pf-realtime-webapp-context.xml",
"classpath:WEB-INF/realtime/comet-config.xml",
"classpath:WEB-INF/realtime/webmvc-config.xml",
"classpath:pfGFIConfigSrvc.xml",
//core
"classpath:META-INF/realtime/pf-realtime-core-context.xml",
//security
"classpath:META-INF/realtime/pf-realtime-security-context.xml",
//prime-locate
"classpath:META-INF/realtime/prime-locate-integration-context.xml",
"classpath:META-INF/realtime/prime-locate-jndi-context.xml",
//prime-locate-cometd
"classpath:META-INF/realtime/prime-locate-cometd-integration-context.xml",
"classpath:META-INF/realtime/prime-locate-cometd-jndi-context.xml",
//prime-notification
"classpath:META-INF/realtime/prime-notification-cometd-integration-context.xml",
"classpath:META-INF/realtime/prime-notification-cometd-jndi-context.xml",
//prime-query
"classpath:META-INF/realtime/prime-query-integration-context.xml",
"classpath:META-INF/realtime/prime-query-jndi-context.xml",
//prime-wire
"classpath:META-INF/realtime/prime-wire-integration-context.xml",
"classpath:META-INF/realtime/prime-wire-jndi-context.xml"
})
#ServletComponentScan
#ComponentScan
#EnableAutoConfiguration(exclude= {
DataSourceAutoConfiguration.class,
JmsAutoConfiguration.class,
MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class,
MultipartAutoConfiguration.class,
SecurityAutoConfiguration.class,
SecurityAutoConfiguration.class,
FallbackWebSecurityAutoConfiguration.class,
OAuth2AutoConfiguration.class})
public class PFRealtimeServicesApplication extends SpringBootServletInitializer implements ServletContextInitializer{
private static final Logger logger = LoggerFactory.getLogger(PFRealtimeServicesApplication.class);
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(PFRealtimeServicesApplication.class);
}
public static void main(String[] args) {
ENVUtils.registerEnvName("env");
ENVUtils.registerRunningSystem("REALTIME");
logger.info("Enter Realtime services application.");
SpringApplication.run(PFRealtimeServicesApplication.class,args);
}
#Override
public void onStartup(ServletContext servletContext) {
ServletRegistration.Dynamic cometdServlet = servletContext.addServlet("cometd", AnnotationCometDServlet.class);
cometdServlet.addMapping("/cometd/*");
cometdServlet.setAsyncSupported(true);
cometdServlet.setLoadOnStartup(1);
//cometdServlet.setInitParameter("PrimeLocateCometDService", PrimeLocateCometDService.class.getName());
//cometdServlet.setInitParameter("PrimeNotificationCometDService", PrimeNotificationCometDService.class.getName());
}
#Bean
protected ServletContextInitializer servletInitializer() {
return servletContext -> servletContext.setAttribute(BayeuxServer.ATTRIBUTE, bayeuxServer(servletContext));
}
#Bean
#DependsOn("servletInitializer")
protected BayeuxServer bayeuxServer(ServletContext servletContext) {
BayeuxServerImpl bean = new BayeuxServerImpl();
bean.setTransports(new WebSocketTransport(bean), new JSONTransport(bean), new JSONPTransport(bean));
servletContext.setAttribute(BayeuxServer.ATTRIBUTE, bean);
bean.setOption(ServletContext.class.getName(), servletContext);
bean.setOption("ws.cometdURLMapping", "/cometd/*");
return bean;
}
}
Hope the PrimeLocateCometDService Bean can be created successfully.
My project use Spring Boot 1.5.19 with CometD 3.0.9 and Tomcat 8.5.37.
(1) #ImportResource import all the xml files to the Application.java
(2) Remove (in xml file)
<bean id="websocketTransport" class="org.cometd.websocket.server.WebSocketTransport">
<constructor-arg ref="bayeux" />
</bean>
(3)Application extends SpringBootServletInitializer implements ServletContextInitializer
#Override
public void onStartup(ServletContext servletContext) {
ServletRegistration.Dynamic cometdServlet = servletContext.addServlet("cometd",
AnnotationCometDServlet.class);
cometdServlet.addMapping("/cometd/*");
cometdServlet.setAsyncSupported(true);
cometdServlet.setLoadOnStartup(1);
}
(4)pom.xml
Remove:
<dependency>
<groupId>org.cometd.java</groupId>
<artifactId>cometd-java-websocket-javax-server</artifactId>
<version>3.0.9</version>
</dependency>
Add:
<dependency>
<groupId>org.cometd.java</groupId>
<artifactId>cometd-java-websocket-javax-server</artifactId>
<version>3.0.9</version>
</dependency>
These are all I think will impact CometD with Spring Boot. Hope these will help someone.

Spring boot: #Repository class not found to inject

This is my Spring boot application related code:
#ComponentScan({"net.gencat.transversal.espaidoc.scheduler", "net.gencat.transversal.espaidoc.backoffice"})
public class SchedulerApplication {//...}
By other hand, I've a repository on package net.gencat.transversal.espaidoc.backoffice.dao:
#Repository
public interface DocumentDAO extends CrudRepository<Document, String> {
}
So, I've a service with a DocumentDAO dependency:
#Service
public class DocumentServiceBackOffice {
private DocumentDAO documentDAO;
public DocumentServiceBackOffice(DocumentDAO documentDAO) {
this.documentDAO = documentDAO;
}
}
However, I'm getting this message:
NoSuchBeanDefinitionException: No qualifying bean of type 'net.gencat.transversal.espaidoc.backoffice.dao.DocumentDAO' available
I've also tried adding #EnableJpaRepositories, but it still doesn't work.
Any ideas?
EDIT
This is my SpringApplication class:
package net.gencat.transversal.espaidoc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
import net.gencat.transversal.espaidoc.common.config.FrontOfficeProperties;
import net.gencat.transversal.espaidoc.common.config.RedisConfiguration;
#SpringBootApplication(exclude = JmxAutoConfiguration.class)
#EnableConfigurationProperties({
FrontOfficeProperties.class
})
#Import(RedisConfiguration.class)
#EnableScheduling
// #ComponentScan("net.gencat.transversal.espaidoc")
//#EnableJpaRepositories
public class SchedulerApplication {
public static void main(String[] args) {
SpringApplication.run(SchedulerApplication.class, args);
}
}
EDIT2:
I've just realized on spring logs that there's some issue related with DocumentDAO:
--- [ main] .RepositoryConfigurationExtensionSupport : Spring Data JPA - Could not safely identify store assignment for repository candidate interface net.gencat.transversal.espaidoc.backoffice.dao.DocumentDAO.
Try adding the following:
#EnableJpaRepositories(basePackages="net.gencat.transversal.espaidoc.backoffice.dao")
public class SchedulerApplication

How to provide custom SSLContext to Netty server on spring boot

How can we configure a custom SSLContext to a spring boot application with Netty server?
From the source code, I see 'reactor.ipc.netty.http.server.HttpServerOptions' which are some server startup options, but I don't find a way to configure them.
Is there any handler through which we can inject our custom SSLContext?
I am looking something similar to this (Spring 5 WebClient using ssl) where WebClient is configured with a custom SSLContext through 'reactor.ipc.netty.http.client.HttpClientOptions'.
Netty can be customized like blow example in spring-boot 2.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.Ssl;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
/**
* author : Mohammad Ghoreishi
*/
#Configuration
#ImportResource({"classpath:convert-iban-service.xml", "classpath:config-loader-context.xml", "classpath*:error-resolver.xml"})
#EnableAutoConfiguration
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
#Bean
public WebServerFactoryCustomizer<NettyReactiveWebServerFactory> customizer(){
return new WebServerFactoryCustomizer<NettyReactiveWebServerFactory>() {
#Override
public void customize(NettyReactiveWebServerFactory factory) {
Ssl ssl = new Ssl();
// Your SSL Cusomizations
ssl.setEnabled(true);
ssl.setKeyStore("/path/to/keystore/keystore.jks");
ssl.setKeyAlias("alias");
ssl.setKeyPassword("password");
factory.setSsl(ssl);
factory.addErrorPages(new ErrorPage("/errorPage"));
}
};
}
}

How to exclude some endpoints from server.servlet.path configuration?

I have Spring Boot application with a single index.html page.
I need to have server.servlet.path=/api setting.
In order to get index.html I have to go localhost:8080/api/ becase of my setting described above.
I want to be able to get index.html by localhost:8080/ and any else endpoints by localhost:8080/api/**.
How can I do it?
Thanks
Once you configured server.servlet.path=/api, DispatcherServlet is going to handle only request matching URL Patterns /api/**.
One way to achieve your requirement is to use a plain Servlet.
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.Charset;
#WebServlet(urlPatterns = {"/"})
public class RootServlet extends HttpServlet {
#Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
ClassPathResource resource = new ClassPathResource("static/index.html");
String content = StreamUtils.copyToString(resource.getInputStream(), Charset.defaultCharset() );
resp.getWriter().write(content);
}
}
Now you can register the Servlet using #ServletComponentScan annotation. Assuming you put RootServlet in com.myapp.servlets package:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
#SpringBootApplication
#ServletComponentScan(basePackages = "com.myapp.servlets")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
For more info see https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-add-a-servlet-filter-or-listener

Unable to access my Spring Boot application from browser

package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#SpringBootApplication
#RestController
public class TrackingManagementSystemApplication {
public static void main(String[] args) {
SpringApplication.run(TrackingManagementSystemApplication.class, args);
}
#RequestMapping("/hello")
public String sayHello(){
return "Hello";
}
}
I ran the above application by using mvn spring-boot:run command it is deploying successfully but when i am trying to access it from browser it is throwing Http 404 exception.
I wanna access it from any web browser.
thank you

Resources