如何用Spring创建restfulweb服务
当我们处理restfulweb服务时,我们需要使用@RestController注释,它基本上代表@Controller和@ResponseBody注释。
当我们在方法中使用@RequestMapping时,我们可以添加一个名为products的属性,它指定发送给用户的输出将是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
controller类将用于处理HTTP请求,这是我们构建restfulweb服务时的惯例。
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);
}
}
让我们分解控制器类。第一眼看上去很简单,但事实上,幕后发生了很多事情。我来解释一下。
多亏了@RequestMapping,我们*将路径“/employee”映射到getEmployeeId方法,如果我们不熟悉@RequestMapping注释,当我们不指定方法请求时,它将默认映射所有HTTP操作。如果我们只想将它作为一个GET方法(在我们的示例中更合适),我们将把上面的代码更改为以下代码:
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”,方法)=请求方法.GET)正如我在本教程开头所说的,我们还指定希望结果是JSON格式的。所以我们可以更改 **@RequestMapping(”/employee“,方法=请求方法.GET)]到 **@RequestMapping(“/employee”,方法)=请求方法.GET,products=“application/json”)。]
我们将查询参数“id”提取为“employeeId”,将“firstName”提取为“fName”,将“lastName”提取为“lName”,然后实例化一个 newEmployee对象,并将获取的id、first name和last name作为查询参数传递。
要检索这些参数并使用此方法创建Employee对象,URL将如下所示:
**http://localhost:8080/员工?id=x&firstName=xx&lastName=xxx]
再次感谢@RequestParam,我们得到了这些x并将它们存储到方法参数中。
运行应用程序
要运行应用程序,我们将使用main()方法,该方法如下所示:
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格式的响应体如下所示:
{
"id":x, firstName="xx", lastName="xxx"
}

