# 如何使用 Spring 创建 RESTful Web 服务 > 原文: [https://javatutorial.net/how-to-create-restful-web-services-with-spring](https://javatutorial.net/how-to-create-restful-web-services-with-spring) 在处理 RESTful Web 服务时,我们需要使用@RestController 注释,该注释基本上表示@Controller 和@ResponseBody 注释。 ![java-featured-image](img/e0db051dedc1179e7424b6d998a6a772.jpg) 当我们在方法中使用@RequestMapping 时,我们可以添加一个名为产生的属性,该属性指定发送给用户的输出将为 JSON 格式。 ### 例 **Employee.java** ```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 服务时的约定。 ```java import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public 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 name return new Employee(employeeId, fName, lName); } } ``` 让我们分解 Controller 类。 乍一看,它看起来很简单,但是在幕后发生了很多事情。 让我确切解释一下。 感谢@RequestMapping,我们将路径“ / employee”映射到我们的 getEmployeeId 方法,如果您不熟悉@RequestMapping 注释,则当我们不指定方法请求时,它将通过 默认。 如果我们只想将它作为 GET 方法使用(在我们的示例中最好),则可以将上面的代码更改为以下代码: ```java import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public 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 name  return 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()方法,该方法如下所示: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` SpringApplication.run()启动应用程序并执行您的所有操作。 这是最简单的方法,但是,如果愿意,可以将其设置为可执行的 JAR,也可以使用 Gradle 或 Maven 从命令行运行它。 你的选择。 ### 响应 JSON 格式的响应主体如下所示: ```java { "id":x, firstName="xx", lastName="xxx" } ```