Spring MVC Concepts: Difference between revisions
Line 38: | Line 38: | ||
A conventional pattern to designate the responsibilities of the handler methods and to associate them with paths and request types is to use [[@RequestMapping]] at class level to configure the path, and then use [[@GetMapping]], [[@PostMapping]], [[@PutMapping]], [[@DeleteMapping]] and [[@PatchMapping]] to assign method-specific handlers: | A conventional pattern to designate the responsibilities of the handler methods and to associate them with paths and request types is to use [[@RequestMapping]] at class level to configure the path, and then use [[@GetMapping]], [[@PostMapping]], [[@PutMapping]], [[@DeleteMapping]] and [[@PatchMapping]] to assign method-specific handlers: | ||
<syntaxhighlight lang='java'> | |||
@Controller | |||
@RequestMapping("/something") | |||
public class SomeController { | |||
@GetMapping | |||
public String read() { | |||
... | |||
} | |||
@PostMapping | |||
public String create() { | |||
... | |||
} | |||
@PutMapping | |||
public String update() { | |||
... | |||
} | |||
} | |||
</syntaxhighlight> | |||
=View= | =View= |
Revision as of 03:13, 12 October 2018
Internal
To Process
TO PROCESS:
- https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/web.html#spring-web
- https://spring.io/guides/gs/rest-service/
- https://spring.io/guides/gs/serving-web-content/
- https://spring.io/guides/tutorials/bookmarks/
- https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications
Model
Controller
A controller is a class that handles requests, fetches and processes data, and then responds with information of some sort. The controller's method handing the requests are named handler methods. Controller classes must be annotated with @Controller.
In case of a browser-facing application, a controller responds by optionally populating model data and passing the request to a view that produces HTML to be returned to the browser. The view that is supposed to render the response is indicated by its logical name, which is returned by the handler method.
@Controller
public class HomeController {
@GetMapping("/")
public String home() {
// returns the view name
return "home";
}
}
In case of a REST application, the controller writes data directly into the body of the response.
A conventional pattern to designate the responsibilities of the handler methods and to associate them with paths and request types is to use @RequestMapping at class level to configure the path, and then use @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping to assign method-specific handlers:
@Controller
@RequestMapping("/something")
public class SomeController {
@GetMapping
public String read() {
...
}
@PostMapping
public String create() {
...
}
@PutMapping
public String update() {
...
}
}
View
The view renders data into HTML format. The view is instantiated dynamically, and its implementation depends on the template engine that is available in the classpath. The template name is derived from the logical name by prefixing it with /templates and postfixing it with .html. Simply placing a <logical-view-name>.html under src/resources/templates makes the template-based view available.
View Logical Name
Project Directory Layout
src/main/resource/static src/main/resource/static/images src/main/resource/templates
Testing MVC Applications
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import static org.hamcrest.Matchers.containsString;
@RunWith(SpringRunner.class)
@WebMvcTest(HomeController.class)
public class HomeControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testHomePage() throws Exception {
mockMvc.perform(get("/")).
andExpect(status().isOk()).
andExpect(view().name("home")).
andExpect(content().string(containsString("Welcome to ...")));
}
}
Spring MVC testing implies using @WebMvcTest. MockMvc mocks the mechanics of Spring MVC, instead of starting a full blown web server.
REST Clients
RestTemplate
TO PROCESS: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#webmvc-resttemplate
POSTing Resource. Data
This overloaded version allows you to receive the newly created resource as a domain model object:
RestTemplate restTemplate = new RestTemplate();
MyResource model = new MyResource(...);
MyResource created = restTemplate.postForObject("http://localhost:8080/myresource", model, MyResource.class);