这是关于 Spring Boot 和 MVC 的第二篇教程。在第一个教程中,我们学习了如何使用 Spring Boot 和 ThymeLeaf 为视图创建一个简单的 MVC 应用程序:使用 Spring Boot 创建一个 MVC 应用程序。在本教程中,我们将使用 REST 控制器增强我们的示例,以便我们的应用程序也可以使用 REST API 访问。
Spring Boot 中的 RESTful Web 服务是开发 Web 应用程序的常见模式,因为它们非常容易,因为您不必处理 HTML 和 CSS。美妙之处在于您可以应用我们为 Web 应用程序学习的相同 MVC 模式。
Spring Boot 的一个优点是它本身支持 JSON,但您也可以将 XML 带入类路径,以便您可以同时支持或仅支持 XML(如果您愿意)。
因此,让我们看一下在我们的应用程序中构建一个 RESTful Web 服务。现在让我们回顾一下我们的领域类:
package com.example;
public class Person {
private String name;
private String surname;
public Person(String name, String surname) {
super();
this.name = name;
this.surname = surname;
}
public Person() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}
我们将添加一个 @Service Bean,它可以注入到我的应用程序和 Web 服务中。
package com.example;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class PersonService {
private static List<Person> persons = new ArrayList<>();
static {
Person p1 = new Person("Jack", "Smith");
Person p2 = new Person("Lucas", "Derrik");
Person p3 = new Person("Andy", "Miller");
persons.add(p1);
persons.add(p2);
persons.add(p3);
}
public List<Person> getAllPersons() {
return persons;
}
}
如您所见,我们的 Service 类有一个用于添加一些 Person 对象的静态块和一个返回 Person 列表的 List 方法。让我们继续构建 RestController:
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api")
public class ApiController {
private PersonService service;
@Autowired
public ApiController(PersonService service) {
super();
this.service = service;
}
@GetMapping("/persons")
public List<Person> getAllPersons() {
return this.service.getAllPersons();
}
}
如您所见,Service 类在 ApiController Rest 类的 Constructor 中是 @Autowired,我们通过 GetMapping “/persons” 公开了 Person 列表。因此,我们现在将在“/api/persons”处使用 Person 对象列表进行响应。
现在我们将继续启动我们的 Web 应用程序并导航到 http://localhost:8080/api/persons。你会看到我返回了一个很好的 XML 有效负载:
请注意,为了启用 XML 表示,Jackson XML 扩展 (jackson-dataformat-xml) 必须存在于类路径中。将以下依赖项添加到您的项目中:
<?xml version="1.0" encoding="UTF-8"?><project>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
</project>
一个资源可以有多种表示形式:
在上面的示例中,浏览器发送了对数据 XML 表示的请求。但是,如果消费者发送带有 Accept 标头为“application/json”的请求,我们需要提供资源的 JSON 表示。让我们看一个使用 cURL 的示例:
$ curl -H "Accept: application/json" http://localhost:8080/api/persons [{"name":"Jack","surname":"Smith"},{"name":"Lucas","surname":"Derrik"},{"name":"Andy","surname":"Miller"}]
以下是如何再次从 shell 请求数据的 XML 表示:
curl -H "Accept: application/xml" http://localhost:8080/api/persons <List><item><name>Jack</name><surname>Smith</surname></item><item><name>Lucas</name><surname>Derrik</surname></item><item><name>Andy</name><surname>Miller</surname></item></List>
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : http://www.masterspringboot.com/develop-applications/rest-services?start=10
内容来源于网络,如有侵权,请联系作者删除!