1

I am working on migrating a j2EE application to spring-boot.. I face here lot of challenges.. need your small help , The below are the steps I followed

  1. Did the setup of POM, JAR and WAR packing
  2. Tried tie convert the Servlet to RestController
  3. Filters I have registered as
@Configuration

public class FilterConfigService {

    @Bean
    public FilterRegistrationBean mdcFilter() {
        FilterRegistrationBean filterRegBean = new FilterRegistrationBean();
        filterRegBean.setFilter(new MDCFilter());
        filterRegBean.addUrlPatterns("/v2/*");
        filterRegBean.setOrder(1);
        return filterRegBean;
    }

    @Bean
    public FilterRegistrationBean apiOriginFilter() {
        FilterRegistrationBean filterRegBean = new FilterRegistrationBean();
        filterRegBean.setFilter(new ApiOriginFilter());
        filterRegBean.addUrlPatterns("/v2/*");
        filterRegBean.setOrder(2);
        return filterRegBean;
    }
}
  1. There was singleton class for instantiating some necessary stuffs through Init() of servlet which I removed as all the beans are by default singleton in scope and did as below to instantiate (Dont know if this is the good practice)
    @Configuration
    public class PdfExtractServerConfig implements ApplicationListener<ContextRefreshedEvent> {

        @Autowired
        PdfExtractServer pdfExtractServer;

        private static Logger log = Logger.getLogger(PdfExtractServerConfig.class);

        @Override
        public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
            try {
                pdfExtractServer.instance(); // this was called in init of servlet
                System.out.println("instance created ..");
            }
            catch (Throwable e) {
                log.error("Unable to start PdfExtractServer", e);
                throw e;
            }
            //Start monitoring for system health
            ResourceCheck.startMonitoring();
            SplunkMgr.instance().addSplunkMessage("BackPressure", LogLevel.INFO);
        }
    }

The single ton class is like as below..

Before it was like

    public static PdfExtractServer instance() {
            if (instance == null) {
                synchronized (startupLock) {
                    if (instance == null) {
                        instance = new PdfExtractServer();
                        instance.start("PES");
                    }
                }
            }
            return instance;
        }

now I made this as

    public static PdfExtractServer instance() {
    //      instance = new PdfExtractServer();
            instance.start("PES");
            return instance;
        }

This is my test controller

    @RestController
    //@RequestMapping("/")
    public class Test {
        @RequestMapping("/test" )
        public String test(){
            return "Tested OK";
        }
    }

I don't understand where do I do wrong .. when I try to run this renders

Unable to start PdfExtractServer java.lang.NullPointerException at com.it.pes.pdfextract.service.PdfExtractServer.instance(PdfExtractServer.java:78) at com.it.pes.pdfextract.config.PdfExtractServerConfig.onApplicationEvent(PdfExtractServerConfig.java:24) at com.it.pes.pdfextract.config.PdfExtractServerConfig.onApplicationEvent(PdfExtractServerConfig.java:13) at com.it.pes.pdfextract.config.PdfExtractServerConfig$$EnhancerBySpringCGLIB$$e703305a.onApplicationEvent() at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:393) at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:347) at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:883) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)

Observation: I tried to register my filters through glassfish jersey, but internallly in some jars , javax.ws.rs-api of 1.1.1 is used so there was conflict and my app was not up, hence to registe the Filters in different way as mentioned above.

Your help would be really great move for me.. thanks in advance..

Package structure is attached here . enter image description here

Bam
  • 79
  • 1
  • 2
  • 11

2 Answers2

0

Your PdfExtractServer object is null when you are asking the container to provide it to you. Your instance object in that class is null.

When a constructor of a class is called which is PdfExtractServer in your case, the @Autowired instance variables do not contain their values yet. As you are dependent on it for the execution of specific logic/or instance creation, I suggest the usage of @PostConstruct annotation. This annotation allows a specific method to be executed after the construction of the instance and also after all the @Autowired instances have been injected. More on this here

The below link should be able to help you understand and possibly fix the null pointer issue.

The usual Autowiring not working issues and it's intended fixes

Sid
  • 339
  • 2
  • 12
0

i agree with the above. I think you are overcomplicating things.

I believe this is all you need. @Configuration annotated classes will be run at server startup. @Bean annotated methods will be executed once and create by default a singleton bean that will be available to @Autowire

All configuration classes will be run first, after that it will scan all packages and insatiate service, component, repositories, controllers.

@Configuration
public class PdfExtractServerConfig {

    @Bean
    public PdfExtractServer pdfExtractServer() {
       final PdfExtractServer pdfExtractServer = new PdfExtractServer();
       pdfExtractServer.start("PES");
       return pdfExtractServer
    }
}

so when you want to use your singelton in any other spring managed bean

// Spring will by default scan after this annotation recursively through
// packages on classes and create by default a singleton. It will also 
// search after all bean dependencies that need to be injected. in this case 
// PdfExtracServer and then inject it.
@Component 
public class Foo {

    @Autowire
    private PdfExtractServer pdfExtractServer;

    //different methods here

}
Toerktumlare
  • 12,548
  • 3
  • 35
  • 54
  • M trying this way.. ll see if it works .. thank yiu so much for your valuable time – Bam Jul 15 '19 at 03:42
  • I tried the same way, got no exception but the controller is not being invoked when I execute the url. Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Mon Jul 15 12:50:42 IST 2019 There was an unexpected error (type=Not Found, status=404). /test – Bam Jul 15 '19 at 07:54
  • How do you invoke it, what address – Toerktumlare Jul 15 '19 at 09:27
  • http://localhost:8080/test is the address for testing, if the controller is invoked or not... – Bam Jul 15 '19 at 11:37
  • what does your packet structure look like – Toerktumlare Jul 15 '19 at 11:38
  • Added the structure – Bam Jul 15 '19 at 11:47
  • first you need to isolate if the controller gets found, you can see this in the spring boot logs when it starts up if it found any controller. second of all i think you need to have `@RequestMapping(value = "/test", method = RequestMethod.GET)` so that it knows its `GET` you want – Toerktumlare Jul 15 '19 at 13:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196499/discussion-between-bam-and-thomas-andolf). – Bam Jul 16 '19 at 02:47