网站建设服务器出租,免费网络验证,深圳特区报,网站如何做ssl认证使用Spring MVC开发Web应用程序的主要工作就是编写Controller逻辑。在Web应用中#xff0c;除了需要使用MVC给用户显示页面外#xff0c;还有一类API接口#xff0c;我们称之为REST#xff0c;通常输入输出都是JSON#xff0c;便于第三方调用或者使用页面JavaScript与之交…使用Spring MVC开发Web应用程序的主要工作就是编写Controller逻辑。在Web应用中除了需要使用MVC给用户显示页面外还有一类API接口我们称之为REST通常输入输出都是JSON便于第三方调用或者使用页面JavaScript与之交互。直接在Controller中处理JSON是可以的因为Spring MVC的GetMapping和PostMapping都支持指定输入和输出的格式。如果我们想接收JSON输出JSON那么可以这样写PostMapping(value /rest,consumes application/json;charsetUTF-8,produces application/json;charsetUTF-8)ResponseBodypublic String rest(RequestBody User user) {return {\restSupport\:true};}对应的Maven工程需要加入Jackson这个依赖com.fasterxml.jackson.core:jackson-databind:2.11.0注意到PostMapping使用consumes声明能接收的类型使用produces声明输出的类型并且额外加了ResponseBody表示返回的String无需额外处理直接作为输出内容写入HttpServletResponse。输入的JSON则根据注解RequestBody直接被Spring反序列化为User这个JavaBean。使用curl命令测试一下$ curl -v -H Content-Type: application/json -d {email:bobexample.com} http://localhost:8080/rest POST /rest HTTP/1.1 Host: localhost:8080 User-Agent: curl/7.64.1 Accept: */* Content-Type: application/json Content-Length: 27 HTTP/1.1 200 Content-Type: application/json;charsetutf-8 Content-Length: 20 Date: Sun, 10 May 2020 09:56:01 GMT{restSupport:true}输出正是我们写入的字符串。直接用Spring的Controller配合一大堆注解写REST太麻烦了因此Spring还额外提供了一个RestController注解使用RestController替代Controller后每个方法自动变成API接口方法。我们还是以实际代码举例编写ApiController如下RestControllerRequestMapping(/api)public class ApiController {AutowiredUserService userService;GetMapping(/users)public List users() {return userService.getUsers();}GetMapping(/users/{id})public User user(PathVariable(id) long id) {return userService.getUserById(id);}PostMapping(/signin)public Map signin(RequestBody SignInRequest signinRequest) {try {User user userService.signin(signinRequest.email, signinRequest.password);return Map.of(user, user);} catch (Exception e) {return Map.of(error, SIGNIN_FAILED, message, e.getMessage());}}public static class SignInRequest {public String email;public String password;}}编写REST接口只需要定义RestController然后每个方法都是一个API接口输入和输出只要能被Jackson序列化或反序列化为JSON就没有问题。我们用浏览器测试GET请求可直接显示JSON响应要测试POST请求可以用curl命令$ curl -v -H Content-Type: application/json -d {email:bobexample.com,password:bob123} http://localhost:8080/api/signin POST /api/signin HTTP/1.1 Host: localhost:8080 User-Agent: curl/7.64.1 Accept: */* Content-Type: application/json Content-Length: 47 HTTP/1.1 200 Content-Type: application/json Transfer-Encoding: chunked Date: Sun, 10 May 2020 08:14:13 GMT{user:{id:1,email:bobexample.com,password:bob123,name:Bob,...注意观察上述JSON的输出User能被正确地序列化为JSON但暴露了password属性这是我们不期望的。要避免输出password属性可以把User复制到另一个UserBean对象该对象只持有必要的属性但这样做比较繁琐。另一种简单的方法是直接在User的password属性定义处加上JsonIgnore表示完全忽略该属性public class User {...JsonIgnorepublic String getPassword() {return password;}...}但是这样一来如果写一个register(User user)方法那么该方法的User对象也拿不到注册时用户传入的密码了。如果要允许输入password但不允许输出password即在JSON序列化和反序列化时允许写属性禁用读属性可以更精细地控制如下public class User {...JsonProperty(access Access.WRITE_ONLY)public String getPassword() {return password;}...}同样的可以使用JsonProperty(access Access.READ_ONLY)允许输出不允许输入。练习小结使用RestController可以方便地编写REST服务Spring默认使用JSON作为输入和输出。要控制序列化和反序列化可以使用Jackson提供的JsonIgnore和JsonProperty注解。