0

I have found a lot of examples that ask similar questions. However, most of the advice says to just use @Controller instead of @RestController when you want to use:

//@Controller
@RestController // @Controller and @ResponseBody into one
public class SreAppController {
    @GetMapping("/")
    public String index() {
        return "index";
    }
}

Since this just returns the String index and does not fetch the HTML file index.html. I know that @ResponseBody, which is in the @RestController tag, renders the output as is from this question. So, I found this tutorial which gives this code:

@RestController
public class SreAppController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @GetMapping("/greeting")
    public Greeting index(@RequestParam(value="name", defaultValue="World") String name){
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }
}

Which I would like to use with AngularJS and format using HTML. This tutorial shows how to consume a RESTful web service, but shows how to consume from http://rest-service.guides.spring.io/greeting. I tried putting localhost:8080/greeting in it's place, but that still just gives a 404 error for localhost:8080 and just returns json for localhost:8080/greeting.

How do I use @RestController with AngularJS and HTML in one application? The intention is to go to localhost:8080 and get the same output as this tutorial:

enter image description here

I know the json from localhost:8080/greeting is coming straight from the Rest controller. I am not sure why the angularJS controller is not found when using localhost:8080.

The tutorial states the need for a minimal amount of web application code so Spring Boot knows to start Tomcat. The suggest app.groovy (shown below) but my rest controller starts the embedded Tomcat server, so I don't think this is the issue.

app.groovy

@Controller class JsApp { }

Javascript and html for reference:

app.js

"use strict";
angular.module('demo', []).controller('app', function($scope, $http) {
        $http.get('/greeting').
            then(function (response) {
            $scope.greeting = response.data;
    });
});

index.html

<!DOCTYPE html>
<html lang="en" ng-app="demo">
<head>
    <meta charset="UTF-8">
    <title>Hello AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js"></script>
    <script src="app.js"></script>
</head>
<body>
    <div ng-controller="app">
        <p>The ID is {{greeting.id}}</p>
        <p>The content is {{greeting.content}}</p>
    </div>
</body>
</html>

Hierarchy of project

enter image description here

8
  • 1
    $http.get('localhost:8080/greeting') should be $http.get('http://localhost:8080/greeting'), or simply $http.get('/greeting'). Open your browser dev tools, and look at the requests you send in the network tab. Also, why use angularjs 1.4.3, and not the latest version? Commented Jun 28, 2018 at 19:31
  • I changed that part of the question to be more descriptive. I changed it to $http.get('/greeting'). I just used it from the tutorial on spring's documents. I updated that know! Thank you for the feedback! Commented Jun 28, 2018 at 19:39
  • So, what exactly is the problem? Is it that you can't even see your HTML page? Where is it located in your project? Commented Jun 28, 2018 at 19:44
  • I cannot get the html file or angularjs file to load which gives the 404 error for localhost:8080. When I add /greeting to it, it goes to the rest controller and displays json. I want to go to localhost:8080 and have angular fetch the data from /greeting and the rest controller, but I am not sure how. Probably something wrong with how I set up Spring boot. The tutorial states that you have to let Spring boot's embedded Tomcat server serve static content and they use a empty controller. I want to use my rest controller. Thanks for your time! Commented Jun 28, 2018 at 20:04
  • 1
    That's the problem.It should be under src/main/resources/public or src/main/resources/static: docs.spring.io/spring-boot/docs/current/reference/htmlsingle/… Commented Jun 28, 2018 at 20:15

1 Answer 1

2

So what you intend to do is accessing your AngularJS application which is inside a Spring Boot application but the index.html doesn't show, innit?

Then you need to know a couple of things:

1) Your AngularJS application must be served as static content.

You can find some examples at this tutorial: Serving Static Web Content with Spring Boot.

Basically, your AngularJS app must be placed inside the Spring Boot app source code in a special folder used for static content - there are several options but tipically it will be named /public or /static.

2) Spring Boot manages all your URLs.

This means the Spring Boot app intercepts all request to your application and then tries to match the URLs against all the existing @Controller.

So if you GET http://<your ip:port>/ and you have a @Controller mapping the root / with GET, then that @Controller will respond to the request, as it happens in the first example of your SreAppController class.

But if you don't then Spring Boot will try to find static content on every one of the special folders for that and - it found - serve it.

Beware you can mix ULRs in Controllers and static web navigations...!

Hope I helped at least a little bit. :-)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.