Sojimanatsu
Sojimanatsu

Reputation: 601

Spring Boot file transformer implementation in java

I have created a standalone java application that transforms several file formats one to another.

So it works over the Main.java like

new DocxToHtmlConverter().docxToHtml("File.docx",".File.html");

I want to implement a spring boot application that does the same thing through localhost url.

I will also implement a selection algorithm on top of this. For example an if clauses to execute the wanted algorithm based on the input string extentions.(can be used endsWith(".docx") etc.

Basically, If i write like localhost:8080/{inputFile,outputFile} and then based on the file extension, related class will be chosen and i should be able to download the resulted file. In this case it is html.

I never used Spring before, so forgive me for my ignorence. I dont even know if this kind of operations are valid with Spring. Thats why I am asking.

What would be the best approach to go with? any kind of helpful links or ideas are highly appreciated.

Upvotes: 0

Views: 439

Answers (1)

nassim
nassim

Reputation: 147

There is many ways to do it. This response describes one solution that lets you keep your library spring agnostic.

You have to create a configuration class that will define a bean with your implementation. You will be able to directly inject this bean into any bean in your spring project

@Configuration
public class DocxToHtmlConverterConfig{
  @Bean
  public DocxToHtmlConverter docxToHtmlConverter(){
    return new DocxToHtmlConverter();
  }
}

This configuration class will create a bean with DocxToHtmlConverter implementation that you can autowayer in any component of your application as follows

@RestController //or @Controller or @{any annotation that declares a component}
public class MyService{
  @Autowired
  DocxToHtmlConverter docxToHtmlConverter;

  public void convertFile(){
    docxToHtmlConverter.docxToHtml("File.docx",".File.html");
  }   
}

Upvotes: 1

Related Questions