【API设计风格—RESTful】:服务端如何编写RESTful风格的API(二)

服务端的WEB架构采用的是SpringMVC。下面我们就直接说如何实现。

【1】Web.xml中的配置

        
         <servlet>
                 <servlet-name>itooApiservlet-name>
                 <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
                 
                 <init-param>
                         <param-name>contextConfigLocationparam-name>
                         <param-value>classpath:springmvc.xmlparam-value>
                 init-param>                 
         servlet>

         <servlet-mapping>
                 <servlet-name>itooApiservlet-name>
                 
                 <url-pattern>/url-pattern>
         servlet-mapping>

【2】springmvc.xml中的配置


<mvc:annotation-driven>mvc:annotation-driven>




<bean class="com.tgb.itoo.controller.CourseController" />        
<bean class="com.tgb.itoo.controller.IndexController" />


<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/jsp/"/>

<property name="suffix" value=".jsp"/>
bean>


<mvc:resources location="WEB-INF/js/" mapping="/js/**"/>
<mvc:resources location="img/" mapping="/img/**"/>

【3】Controller中方法的写法

package com.tgb.itoo.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

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.tgb.itoo.vo.Course;

/**
 * @author xuchenyang 
 * @version 2016年3月19日16:35:25
 */
@Controller
public class CourseController {
    /**
     * 获得一门课程
     * @param studentId
     * @param courseid
     * @return
     */
    @RequestMapping(value="/student/{studentId}/course/{courseId}",method=RequestMethod.GET)
    @ResponseBody
    public Course GetCourse(@PathVariable("studentId") String studentId,@PathVariable("courseId") String courseid){     
        Course course=new Course();
        course.setId(courseid);
        course.setCode("0002");
        course.setCourseName("高等数学");
        //response.setHeader("Access-Control-Allow-Origin", "*"); //允许哪些url可以跨域请求到本域
        return course;
    }
    /**
     * 获得课程列表
     * @return
     */
    @RequestMapping(value="/student/{id}/course",method=RequestMethod.GET)
    @ResponseBody 
    public List GetCourseList(@PathVariable("id") String studentId){
        //定义课程
        Course course1=new Course();
        course1.setId("1");
        course1.setCode("0001");
        course1.setCourseName("大学语文");
        //定义课程
        Course course2=new Course();
        course2.setId("2");
        course2.setCode("0002");
        course2.setCourseName("高等数学");
        //将课程添加到集合中并返回
        List courseList=new ArrayList();
        courseList.add(course1);
        courseList.add(course2);
        return courseList;

    }
    /**
     * 添加一门课程
     * @return
     */
    @RequestMapping(value="/student/{studentId}/course",method=RequestMethod.POST)
    @ResponseBody
    public  String SaveCourse(@PathVariable("studentId") String studentId,@RequestBody Course course){
        //保存course对象到数据库    
        //给课程生成ID
        course.setId("8");
        System.out.println("courseid:"+course.getId()+" code:"+course.getCode()+" name:"+course.getCourseName());
        StringBuilder sb=new StringBuilder();
        sb.append("{\"result\":true}");
        return sb.toString();
    }
    /**
     * 修改一门课程
     * @return
     */
    @RequestMapping(value="/student/{studentId}/course/{courseId}",method=RequestMethod.PUT)
    @ResponseBody
    public  String UpdateCourse(@PathVariable("studentId") String studentId,@PathVariable("courseId") String courseid,@RequestBody Course course){
        //保存course对象到数据库        
        //course.setId(courseid);
        System.out.println("courseid:"+course.getId()+" code:"+course.getCode()+" name:"+course.getCourseName());
        StringBuilder sb=new StringBuilder();
        sb.append("{\"result\":true}");
        return sb.toString();
    }
    /**
     * 删除一门课程
     * @return
     */
    @RequestMapping(value="/student/{studentId}/course/{courseId}",method=RequestMethod.DELETE)
    @ResponseBody
    public  String DeleteCourse(@PathVariable("studentId") String studentId,@PathVariable("courseId") String courseid){     
        StringBuilder sb=new StringBuilder();
        System.out.println("删除的课程的id:"+courseid);
        sb.append("{\"result\":true}");
        return sb.toString();
    } 
}

【注意】:
@RequestBody:表示传入的是JSON格式的数据
@ResponseBody:表示返回的是JSON格式的数据
@RequestMapping(value=”/student/{studentId}/course/{courseId}”,method=RequestMethod.GET)表示请求的路径和请求的类型
@PathVariable:用此注解来表示方法 中的参数和RequestMapping中的请求路径中的参数的对应关系。PathVariable(“studentId”) 括号中的名称必须和路径中的参数占位符的名称一致。但是方法中参数的名称任意。

【4】测试

服务端如何去测试给移动端提供的接口返回的数据是否正确?可以写一个JSP页面通过$.Ajax测试不同类型的请求

你可能感兴趣的:(……【API设计风格】,▶【其他】)