博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring MVC风格的restful接口开发
阅读量:6758 次
发布时间:2019-06-26

本文共 4574 字,大约阅读时间需要 15 分钟。

hot3.png

项目原本是springmvc项目,新增了resuful接口服务端。

205230_i256_2499253.png

 

IUserService.java

package com.yq.publicproject.server;import javax.ws.rs.core.MediaType;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import com.yq.publicproject.model.User;/** * spring MVC风格的restful接口  * @author Administrator * */@RequestMapping("/userService")  @Controller public interface IUserService {	@RequestMapping(value = "/hello", produces = "text/plain;charset=UTF-8")      public @ResponseBody      String hello();              @RequestMapping(value = "/say/{msg}", produces = "application/json;charset=UTF-8")      public @ResponseBody      String say(@PathVariable(value = "msg") String msg);              @RequestMapping(value = "/user/{id:\\d+}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")      public @ResponseBody      String getUser(@PathVariable("id") int id);                  @RequestMapping(value = "/user/{id:\\d+}", method = RequestMethod.DELETE)      public @ResponseBody Object deleteUser(@PathVariable("id") int id) ;            /**      * 推荐使用,这种可以解决绝大多数问题      * @param person      * @return      */      @RequestMapping(value = "/user", method = RequestMethod.POST,               produces = {MediaType.APPLICATION_JSON,"application/json;charset=UTF-8"},              consumes = {MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})      public String addUser(User user);                  @RequestMapping(value = "/user", method = RequestMethod.PUT)      public @ResponseBody Object updateUser(@RequestBody User user);}

 

UserService.java

package com.yq.publicproject.server.impl;import org.apache.log4j.Logger;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.ResponseBody;import com.alibaba.fastjson.JSONObject;import com.yq.publicproject.model.User;import com.yq.publicproject.server.IUserService;/** * spring MVC风格的restful接口  * @author Administrator * */@Controllerpublic class UserService implements IUserService {		/** 日志实例 */      private static final Logger logger = Logger.getLogger(UserService.class);      public @ResponseBody      String hello() {          logger.info("hello........");          return "你好!hello";      }        public @ResponseBody      String say(@PathVariable(value = "msg") String msg) {          return "{\"msg\":\"you say:'" + msg + "'\"}";      }        public @ResponseBody      String getUser(@PathVariable("id") int id) {          logger.info("获取人员信息id=" + id);          User user = new User();          user.setUserName("张三");          user.setUserId(id+"");                    String jsonObject = JSONObject.toJSONString(user);          logger.info(jsonObject);          return jsonObject;      }            public Object deleteUser(@PathVariable("id") int id) {          logger.info("删除人员信息id=" + id);          JSONObject jsonObject = new JSONObject();              jsonObject.put("msg", "删除人员信息成功");      return jsonObject;      }          public @ResponseBody String addUser(@RequestBody User user) {          logger.info("注册人员信息成功id=" + user.getUserId());          JSONObject jsonObject = new JSONObject();          jsonObject.put("msg", "注册人员信息成功");          return jsonObject.toString();      }            public @ResponseBody Object updateUser(@RequestBody User user) {          logger.info("更新人员信息id=" + user.getUserId());          JSONObject jsonObject = new JSONObject();          jsonObject.put("msg", "更新人员信息成功");          return jsonObject.toString();      }}

 

因为web.xml进行了如下配置,所有访问url都加上.htmls

spring mvc servlet
rest
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:config/spring-mvc.xml
1
rest
*.htmls

启动Tomcat服务,输入接口访问url。

1.http://localhost:8080/public_project/userService/hello.htmls

205938_CNmv_2499253.png

2.http://localhost:8080/public_project/userService/say/%E4%BD%A0%E8%BF%99%E4%B8%AA%E7%AC%A8%E8%9B%8B.htmls

205959_8jDd_2499253.png

3.http://localhost:8080/public_project/userService/user/1.htmls

id 为 1 有数据。

210021_LW9i_2499253.png

转载于:https://my.oschina.net/yq0128/blog/806033

你可能感兴趣的文章
好详细啊saltsatck超全配置
查看>>
安装LAMP环境遇到Sorry, I cannot run apxs
查看>>
centos7双网卡bond失败
查看>>
JNI AES文件及字符串加解密
查看>>
APUE读书笔记-16网络通信-01简介
查看>>
apache站点稍大文件不完整原因及解决
查看>>
python的reduce函数
查看>>
细读shell-6
查看>>
ubuntu11.10安装php mysql wordpress
查看>>
一、2 基于wsgiref定义自己的web框架
查看>>
Ubuntu Server14.04 32位安装odoo8.0简单方法
查看>>
jQuery-easyui下的多表关联的增删改操作
查看>>
我的友情链接
查看>>
兼容IE,Firefox,CSS3 opacity透明度
查看>>
读取Hive中所有表的表结构,并在新Hive库中创建表,索引等
查看>>
XenServer部署系列之02——系统安装及许可
查看>>
linux下FTP服务器搭建
查看>>
程序的查询 ps - 笔记1
查看>>
Conversion to Dalvik format failed with error 1的又一种情形
查看>>
nodejs抓取数据二(列表解析)
查看>>