1

I created a springboot mutimodule project with the help of tutorial two modules -one backend(java classes), another forntend(angular 5 application) I have included a dependency of frontend module in Backend module. I am creating a jar using maven resource plugin. I am copying static resources to static folder of build directory in my pom.xml too. I also have a @Controller which returns "index". When I run jar I expect to see index.html(of front end module) to render on localhost:8080. But I get an Internal server error saying "template resolver could not find index. I know @Contoller renders an HTML from templates folder, but in my case I want it to be rendered from frontend module.

Here is my pom.xml for frontend module is -

<build>
    <plugins>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${build.directory}/classes/static</outputDirectory>
                        <resources>
                            <resource>
                                <directory>target/frontend</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>1.6</version>

            <configuration>
                <nodeVersion>v8.10.0</nodeVersion>
                <npmVersion>5.6.0</npmVersion>
                <workingDirectory>src/main/frontend</workingDirectory>
            </configuration>

            <executions>
                <execution>
                    <id>install node and npm</id>
                    <goals>
                        <goal>install-node-and-npm</goal>
                    </goals>
                </execution>

                <execution>
                    <id>npm install</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                </execution>

                <execution>
                    <id>npm run build</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>

                    <configuration>
                        <arguments>run build</arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
    <!-- <resources> -->
    <!-- <resource> -->
    <!-- <directory>target/frontend</directory> -->
    <!-- <targetPath>static</targetPath> -->
    <!-- </resource> -->
    <!-- </resources> -->
</build>

I am new to springboot, have no idea what I am doing wrong. Please help

  • This post may have a solution to your problem https://stackoverflow.com/questions/27381781/java-spring-boot-how-to-map-my-app-root-to-index-html – Ahmed Itani May 20 '18 at 09:55
  • @AhmedItani - this post would have helped if my application would have been an MVC application, but I do not have views in my Springboot project instead those htmls are part of angular 5 application in a separate module. – Ruchi Pareek May 21 '18 at 06:45

2 Answers2

1

Made following changes and it worked - 1. Removed maven resource plugin from pom instead just added

<resources>
    <resource>
    <directory>target/frontend</directory>
    <targetPath>static</targetPath>
    </resource>
</resources>

and I could see the spring security browser login popup when I hit localhost:8080/

0

Try this it works fine for me.

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
public class TestController {

    @RequestMapping(method = RequestMethod.GET)
    public void getIndex(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String uri = request.getRequestURI();
        String accessResource = "/static/index.html";
        if(uri.contains(".")) { // if css or jpg or font or others
            accessResource = "/static" + uri;
        }
        Resource resource;
        ByteArrayOutputStream bos;
        resource = new ClassPathResource(accessResource, TestController.class);
        bos = new ByteArrayOutputStream();
        FileCopyUtils.copy(resource.getInputStream(), bos);
        response.getOutputStream().write(bos.toByteArray());
    }
}