Spring 5 controller return html string to load in Browser

The old code that I had was a Java Servlet which took in some parameters and pushed out an HTML string:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Delegate the action
    doAction(request, response);

The ‘doAction’ method did something like this:

public void doAction(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Read the template EHR HTML file
    String html = FileUtils.readFileToString(new File(getServletContext().getRealPath("/viewer.html")), "utf-8");
     ... make some changes to html ...
     PrintWriter out = response.getWriter();

And this sent an HTML string to the browser and all the relative locations worked. I mean we have a directory structure where we have:


An this page loads, and all the loaded js and css and img files all worked great. I should say the call to this is like:
http://localhost:8080/webapp/servlet?{some parameters)

Now, we are using Spring 5 and I have lots of experience setting up Spring and creating RESTful end-points that spit out JSON. I call a new Spring Controller passing in variables, and the back-end logic all works. Now I want to output HTML the same way the old servlet worked. This is what I have now.

public class ViewerController
    @GetMapping(value = "/viewer", produces = MediaType.TEXT_HTML_VALUE)
    public @ResponseBody String getPatientViewerData(
@RequestParam(value = "token", required = true) String token, 
@RequestParam(value = "myid", required = true) String myid)
    String html = "";
    try {
        html = service.getHtmlFromBusinessLogic();
    catch (Exception e) {
    return html;

When I test this, I DO get HTML returned to my String, but all the relative links are thrown off, so if I my call to this controller is:
http://localhost:8080/webapp/api/controller?{some parameters)
Then all my relative links are looking for:
http://localhost:8080/webapp/api/js/somejs.js or

The Application Initializer in this Spring 5 webapp is as follows:

public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

private static final Log logger = LogFactory.getLog(ApplicationInitializer.class);

protected Class<?>[] getRootConfigClasses() {
    return new Class[]
    { ViewerAppConfig.class };

protected Class<?>[] getServletConfigClasses() {
    return new Class[]

    protected String[] getServletMappings() {
       return new String[] { "/api/*" };


So, I am sure the easiest solution would be to remove the “/api/ for any rest calls here. I was hoping there would be another solution, but I am not sure if there is any.

Any help would be great. Thanks!

Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
newest oldest most voted
Notify of

you’re code looks ok. Maybe the only thing you need to define is how to serve static resources in your Spring config. Something like this: @Configuration @EnableWebMvc public class MvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**") // your prefered mapping, for example web app root .addResourceLocations("/resources/"); // project files location } } This way you tell Spring where to expose all your css, js, etc... also you don’t need to change your DispatcherServlet url mapping.