Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Commit 80caa42

Browse files
author
Taras
committed
Spring MVC task
1 parent 13f594e commit 80caa42

8 files changed

Lines changed: 114 additions & 0 deletions

File tree

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ include 'spring-framework-ioc-advanced'
55
include 'spring-framework-mvc-thymeleaf'
66
include 'spring-framework-model'
77
include 'spring-framework-ioc-task'
8+
include 'spring-framework-mvc-task'
89

spring-framework-ioc-task/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@ This application is based on two abstraction `StringConsumerService`, and `Sprin
55

66
Your job is to **configure application context**, so the `StringConsumerService#comnume()` will print a spring in upper case
77
with a space after each letter. See details in the 'todo' code comments. An entry point is a main class `AppContextTaskConfig`
8+
9+
_See related examples:_
10+
* https://github.com/BobocodeCourse/spring-framework/tree/master/spring-framework-ioc-basics
11+
* https://github.com/BobocodeCourse/spring-framework/tree/master/spring-framework-ioc-advanced
12+
813

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Spring MVC - Sum controller task
2+
3+
This is a web application that provides an endpoint to calculate a mathematical sum. Thus, you can **calculate a sum of a**,
4+
and b using the following URL `'/sum/a/b'`, and application should return you the result message.
5+
6+
E.g. `http://localhost:8080/sum/12/98` should respond with the message `12 + 98 = 110`
7+
8+
The described logic should be implemented in `SumController`, and you job is to
9+
* **finish controller implementation**
10+
11+
_All other required configurations are already implmented. To test this application, you need to deploy it to the Servlet
12+
container (e.g. Tomcat)_
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
group 'com.bobocode'
2+
version '1.0-SNAPSHOT'
3+
4+
apply plugin: 'java'
5+
apply plugin: 'war'
6+
7+
sourceCompatibility = 1.8
8+
9+
repositories {
10+
mavenCentral()
11+
}
12+
13+
dependencies {
14+
compile 'org.projectlombok:lombok:1.16.12'
15+
16+
compile 'org.springframework:spring-context:4.3.3.RELEASE'
17+
compile 'org.springframework:spring-webmvc:4.3.8.RELEASE'
18+
19+
providedCompile "javax.servlet:javax.servlet-api:3.1.0"
20+
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.bobocode.configs;
2+
3+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4+
5+
6+
public class BobocodeWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
7+
@Override
8+
protected Class<?>[] getRootConfigClasses() {
9+
return new Class[]{RootConfig.class};
10+
}
11+
12+
@Override
13+
protected Class<?>[] getServletConfigClasses() {
14+
return new Class[]{WebConfig.class};
15+
}
16+
17+
@Override
18+
protected String[] getServletMappings() {
19+
return new String[]{"/"};
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.bobocode.configs;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.ComponentScan.Filter;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.FilterType;
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
10+
11+
import java.util.function.BiFunction;
12+
13+
@Configuration
14+
@ComponentScan(basePackages = "com.bobocode", excludeFilters = {
15+
@Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class),
16+
@Filter(type = FilterType.ANNOTATION, value = Controller.class)
17+
})
18+
public class RootConfig {
19+
@Bean
20+
public BiFunction<Integer, Integer, Integer> sumFunction() {
21+
return (a, b) -> a + b;
22+
}
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.bobocode.configs;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
6+
7+
@Configuration
8+
@ComponentScan(basePackages = "com.bobocode.controller")
9+
@EnableWebMvc
10+
public class WebConfig {
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.bobocode.controller;
2+
3+
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
7+
import java.util.function.BiFunction;
8+
9+
// todo: 1 - declare this class as Spring MVC controller
10+
public class SumController {
11+
12+
@Autowired // this bean is already configured in RootConfig.java
13+
private BiFunction<Integer, Integer, Integer> integerBiFunction;
14+
15+
// todo: 2 - specify proper url mapping ('/sum/a/b', where a, and b is a pass variable)
16+
// todo: 3 - declare that this method provides a response body that should be returned to the client
17+
public String compute(@PathVariable Integer a, @PathVariable Integer b) {
18+
return a + " + " + b + " = " + integerBiFunction.apply(a, b);
19+
}
20+
}

0 commit comments

Comments
 (0)