Serving web content with Spring MVC

In Spring MVC, HTTP requests are handled by controllers. Controllers are classes that have been annotated with @Controller—similar to how we annotate rest controllers with @RestController. The best way to understand the way controllers work is to have an example to scrutinize. Let us create a simple Spring MVC controller that handles HTTP GET requests sent to the /say/hello path by returning a view, which has the responsibility of rendering an HTML page to a user.

Create a controller package in com.example.placereviewer and add the following class to it:

package com.example.placereviewer.controller

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping

@Controller
@RequestMapping("/say")
class HelloController {

@GetMapping("/hello")
fun hello(): String {
return "hello"
}
}

As can be seen, the creation of a controller is in no way a complex task. The annotation of HelloController with @Controller tells Spring that this class is a Spring MVC controller and as such is capable of handling HTTP requests. In addition, annotating HelloController with @RequestMapping("/say") specifies that the controller handles HTTP requests that have /say as their base paths. We defined a hello() action within the controller. Since this action was annotated with @GetMapping("/hello"), it handles GET requests to the path /say/hello. The string returned by hello() is the name of the view resource that should be rendered to the client upon the sending of a request to this route.

Since hello() requires that a view named hello is returned to the client, our next task is to add such a view to our project. Views are generally added to the templates folder of a Spring project's resources directory. Add a hello.html file to the project by right-clicking on the templates and then selecting New | HTML File:

You will be prompted to provide a name for the HTML file to be created. Input hello as the name and proceed:

IntelliJ IDEA will generate an HTML file in the selected directory. Once this is done, modify its content to contain the basic HTML, as shown here:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
Hello world!
</body>
</html>

We are now ready to test if the controller we created works. We will know if it works if it returns an HTML page with a message reading Hello World! when we send a GET request to its route path. Before we forget, we must add GET requests sent to /say/hello as requests permitted by Spring Security without authentication. Doing this is straightforward; simply modify configure(HttpSecurity) in WebSecurityConfig to permit GET requests to /say/hello, as shown in the following snippet:

@Throws(Exception::class)
override fun configure(http: HttpSecurity) {
http.authorizeRequests()
.antMatchers(HttpMethod.GET,"/say/hello").permitAll() // added line
.antMatchers(HttpMethod.GET,"/register").permitAll()
.antMatchers(HttpMethod.POST,"/users/registrations").permitAll()
.antMatchers(HttpMethod.GET, "/css/**").permitAll()
.antMatchers(HttpMethod.GET, "/webjars/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.successHandler { request, response, _ ->
redirectStrategy.sendRedirect(request, response, "/home")
}
.permitAll()
.and()
.logout()
.permitAll()
}

Build and run the Spring application, and then open your favorite web browser and navigate to the following URL: http://localhost:5000/say/hello.

You will be greeted enthusiastically with a Hello World! message.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.188.201