项目原本是springmvc项目,新增了resuful接口服务端。
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
*
*/
@Controller
public 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
<servlet>
<description>spring mvc servlet</description>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/spring-mvc.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>*.htmls</url-pattern>
</servlet-mapping>
启动Tomcat服务,输入接口访问url。
1.http://localhost:8080/public\_project/userService/hello.htmls
3.http://localhost:8080/public\_project/userService/user/1.htmls
id 为 1 有数据。