1package com.zetcode.model;
2
3public class Country {
4
5 private String name;
6 private int population;
7
8 public String getName() {
9 return name;
10 }
11
12 public void setName(String name) {
13 this.name = name;
14 }
15
16 public int getPopulation() {
17 return population;
18 }
19
20 public void setPopulation(int population) {
21 this.population = population;
22 }
23}
24
1package com.zetcode;
2
3import org.springframework.boot.SpringApplication;
4import org.springframework.boot.autoconfigure.SpringBootApplication;
5
6@SpringBootApplication
7public class Application {
8
9 public static void main(String[] args) {
10 SpringApplication.run(Application.class, args);
11 }
12}
13
1package com.zetcode.controller;
2
3import com.zetcode.bean.Country;
4import org.springframework.http.HttpHeaders;
5import org.springframework.http.ResponseEntity;
6import org.springframework.stereotype.Controller;
7import org.springframework.web.bind.annotation.RequestMapping;
8import org.springframework.web.bind.annotation.ResponseBody;
9
10@Controller
11public class MyController {
12
13 @RequestMapping(value = "/getCountry")
14 public ResponseEntity<Country> getCountry() {
15
16 var c = new Country();
17 c.setName("France");
18 c.setPopulation(66984000);
19
20 var headers = new HttpHeaders();
21 headers.add("Responded", "MyController");
22
23 return ResponseEntity.accepted().headers(headers).body(c);
24 }
25
26 @RequestMapping(value = "/getCountry2")
27 @ResponseBody
28 public Country getCountry2() {
29
30 var c = new Country();
31 c.setName("France");
32 c.setPopulation(66984000);
33
34 return c;
35 }
36}
37
1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="http://maven.apache.org/POM/4.0.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5 http://maven.apache.org/xsd/maven-4.0.0.xsd">
6 <modelVersion>4.0.0</modelVersion>
7
8 <groupId>com.zetcode</groupId>
9 <artifactId>responseentityex</artifactId>
10 <version>1.0-SNAPSHOT</version>
11 <packaging>jar</packaging>
12
13 <properties>
14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15 <maven.compiler.source>11</maven.compiler.source>
16 <maven.compiler.target>11</maven.compiler.target>
17 </properties>
18
19 <parent>
20 <groupId>org.springframework.boot</groupId>
21 <artifactId>spring-boot-starter-parent</artifactId>
22 <version>2.1.0.RELEASE</version>
23 </parent>
24
25 <dependencies>
26 <dependency>
27 <groupId>org.springframework.boot</groupId>
28 <artifactId>spring-boot-starter-web</artifactId>
29 </dependency>
30 </dependencies>
31
32 <build>
33 <plugins>
34 <plugin>
35 <groupId>org.springframework.boot</groupId>
36 <artifactId>spring-boot-maven-plugin</artifactId>
37 </plugin>
38 </plugins>
39 </build>
40
41</project>
42
1@RequestMapping(value = "/getCountry2")
2@ResponseBody
3public Country getCountry2() {
4
5 var c = new Country();
6 c.setName("France");
7 c.setPopulation(66984000);
8
9 return c;
10}
11