Create a new servlet to be used along Vaadin 8

Hello folks,

this is my first post here, so my apologies if this is not the right place to put this question, or if the technical terms are not the most accurate.

I need to create a servlet that should be used within a vaadin 8 + spring-boot application. I have already found out how to create a custom servlet to replace the vaadin one, but what a really need is to develop another servlet that will send a response to the requester in a very specific way. Bellow you can find the code of the test servlet i’ve created. Please bear in mind that this is a test Servlet, so the code is a little bit ugly. As soon as I get it to work (i.e. get registered and available), it will properly rewritten.

package cl.vmetrix.vcube.viewlayer.app.cfg;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.jdbc.Work;
import org.springframework.stereotype.Component;

import com.flexmonster.compressor.Compressor;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinServlet;
import com.vaadin.spring.server.SpringVaadinServlet;

import cl.vmetrix.persistencelayer.database.DatabaseBO;
import cl.vmetrix.vcube.viewlayer.flexmonster.FlexmonsterUI;
import cl.vmetrix.vcube.viewlayer.ui.VCubeUI;

@WebServlet(value = "/vqueryPivotData/*", asyncSupported = true)
@Component("flexmonsterServlet")
//@VaadinServletConfiguration(productionMode = true, ui = FlexmonsterUI.class)
public class FlexmonsterCompressorServlet extends  VaadinServlet {

	private static final long serialVersionUID = -4997305985202070360L;
	
    @Override
    protected void servletInitialized() throws ServletException {
        super.servletInitialized();
    }
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			
			DatabaseBO db = DatabaseBO.getDatabaseFromPool( "VCUBE" );
			
			Session session = db.getConnection();
			
			session.doWork(new Work() {
			    @Override
			    public void execute(Connection connection) throws SQLException {
			    	String query = "Select * from  VCUBE_USER.MAPPINGTEST1";
			    	Statement statement = connection.createStatement();
					
					ResultSet resultSet = statement.executeQuery(query);
					
					InputStream inputStream = Compressor.compressDb(resultSet);
					
					response.setContentType("text/plain");

					try {
						OutputStream outputStream = response.getOutputStream();
						int length = 0;
						byte[] buffer = new byte[102400]
; //100mb
						while ((length = inputStream.read(buffer)) > 0) {
							outputStream.write(buffer, 0, length);
							outputStream.flush();
						}
					} catch (Throwable e) {
						e.printStackTrace();
					}

			    	
			    }
			});

			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Thanks in advance

An additional info: In the logs I can see the following line, meaning (according to my understanding) that the servlet is loaded, but not with the expected context:

2018-09-06 13:49:47 INFO  ServletRegistrationBean:190 - Mapping servlet: 'flexmonsterServlet' to 
[/]

Got it working… Here’s the approach in case someone needs it in the future:

1- Implemented a Servlet extending VaadinServlet, overriding the service method:

public class CustomServlet extends  VaadinServlet {

    @Override
    protected void service(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {}
}

2- Created a dummy UI

public class CustomServletUI extends UI{

	@Override
	protected void init(VaadinRequest request) {
		System.out.println( "init" );
		
		
	}
	

}

3- Registered the servlet manually in one of my configuration classes:

@Configuration
public class ApplicationConfiguration {

	@Bean	
	public ServletRegistrationBean flexmonsterServlet() {
		   ServletRegistrationBean servRegBean = new ServletRegistrationBean();
		   servRegBean.setServlet(new FlexmonsterCompressorServlet());
		   servRegBean.addUrlMappings("/queryPivot/*");
		   servRegBean.addInitParameter("UI", "com.custom.CustomServletUI");
		   servRegBean.setLoadOnStartup(1);
		   return servRegBean;
	}
}

Thanks! it worked!