原文: https://javatutorial.net/how-to-create-restful-web-services-with-spring
在处理 RESTful Web 服务时,我们需要使用@RestController注释,该注释基本上表示@Controller和@ResponseBody注释。

当我们在方法中使用@RequestMapping时,我们可以添加一个名为产生的属性,该属性指定发送给用户的输出将为 JSON 格式。
示例
Employee.java
public class Employee {private int id;private String firstName;private String lastName;public Employee(int id, String firstName, String lastName) {this.id = id;this.firstName = firstName;this.lastName = lastName;}public setFirstName(String fName) {firstName = fName;}public setLastName(String lName) {lastName = lName;}public int getId() {return id;}public String getFirstName() {return firstName;}public String getLastName() {return lastName;}}
EmployeeController.java
控制器类将可用于处理 HTTP 请求,这是我们构建 RESTful Web 服务时的约定。
import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class EmployeeController {@RequestMapping("/employee")public Employee createEmployee(@RequestParam(value="id") int employeeId, @RequestParam(value="firstName") String fName, @RequestParam(value="lastName") String lName) {// return the new Employee object with that id, first name and last namereturn new Employee(employeeId, fName, lName);}}
让我们分解Controller类。 乍一看,它看起来很简单,但是在幕后发生了很多事情。 让我确切解释一下。
感谢@RequestMapping,我们将路径/employee映射到我们的getEmployeeId方法,如果您不熟悉@RequestMapping注释,则当我们不指定方法请求时,默认情况下它将通过。 如果我们只想将它作为 GET 方法使用(在我们的示例中最好),则可以将上面的代码更改为以下代码:
import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class EmployeeController {@RequestMapping("/employee", method=RequestMethod.GET)public Employee createEmployee(@RequestParam(value="id") int employeeId, @RequestParam(value="firstName") String fName, @RequestParam(value="lastName") String lName) {// return the new Employee object with that id, first name and last namereturn new Employee(employeeId, fName, lName);}}
因此现在我们将@RequestMapping注解更改为@RequestMapping("/employee", method=RequestMethod.GET)。正如我在本教程开始时所说的,我们还指定了我们希望结果为 JSON 格式。 因此我们可以将@RequestMapping("/employee", method=RequestMethod.GET)更改为@RequestMapping("/employee", method=RequestMethod.GET, Produces="application/json")。
我们将查询参数id提取到employeeId,将firstName提取到fName,将lastName提取到lName,然后实例化一个新的Employee对象并首先传递该 ID。 我们作为查询参数得到的名字和姓氏。
要检索这些参数并使用此方法创建Employee对象,URL 如下所示:
http://localhost:8080/employee?id=x&firstName=xx&lastName=xxx
再一次,感谢@RequestParam,我们得到了x,并将它们存储在方法参数中。
运行应用程序
要运行该应用程序,我们将使用main()方法,该方法如下所示:
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}
SpringApplication.run()启动应用程序并执行您的所有操作。 这是最简单的方法,但是,如果愿意,可以将其设置为可执行的 JAR,也可以使用 Gradle 或 Maven 从命令行运行它。 你的选择。
响应
JSON 格式的响应主体如下所示:
{"id":x, firstName="xx", lastName="xxx"}
