idea+springmvc+maven学习http接口

12 篇文章 0 订阅
10 篇文章 0 订阅

学习了几种方式。采用@RequestBody注解的方式最简单。

一、通过maven的pom.xml文件导入jackson的包。

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.7.4</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.7.4</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>2.7.4</version>
</dependency>
二、配置servlet.xml文件

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <!--json转换器-->
            <ref bean="mappingJacksonHttpMessageConverter" />
        </list>
    </property>
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="mappingJacksonHttpMessageConverter" />
        </list>
    </property>
</bean>

<bean id="mappingJacksonHttpMessageConverter"
      class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name = "supportedMediaTypes">
        <list>
            <bean class="org.springframework.http.MediaType">
                <constructor-arg index="0" value="text"/>
                <constructor-arg index="1" value="plain"/>
                <constructor-arg index="2" value="UTF-8"/>
            </bean>
            <bean class="org.springframework.http.MediaType">
                <constructor-arg index="0" value="*"/>
                <constructor-arg index="1" value="*"/>
                <constructor-arg index="2" value="UTF-8"/>
            </bean>
            <bean class="org.springframework.http.MediaType">
                <constructor-arg index="0" value="text"/>
                <constructor-arg index="1" value="*"/>
                <constructor-arg index="2" value="UTF-8"/>
            </bean>
            <bean class="org.springframework.http.MediaType">
                <constructor-arg index="0" value="application"/>
                <constructor-arg index="1" value="json"/>
                <constructor-arg index="2" value="UTF-8"/>
            </bean>
        </list>
    </property>
</bean>
三、写接口,采用注解的方式

1.请求地址:http://192.168.2.23:8080/addstudent?username=liudehua&age=60

参数username和age要对应。

@RequestMapping(value = "addstudent",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public  Object addstudent(String username,int age){

//        返回数组
        List<Student> studentList = new ArrayList<Student>();
        Student stu = new Student();
        stu.setName(username);
        stu.setAge(age);
        studentList.add(stu);

        stu = new Student();
        stu.setName("wushan");
        stu.setAge(20);
        studentList.add(stu);
        Map<String,List<Student>> map = new HashMap<String,List<Student>>();
        map.put("studentList",studentList);
        return map;
    }

2.请求地址:http://192.168.2.23:8080/addstudent?username=liudehua&age=60

//通过HttpServletRequest接收,post方式和get方式都可以。
@RequestMapping(value = "addstudent",produces = "application/json;charset=UTF-8")
@ResponseBody
public Object addstudent(HttpServletRequest request) {
    String username = request.getParameter("username");
    int age = Integer.valueOf(request.getParameter("age"));
    Student student = new Student();
    student.setName(username);
    student.setAge(age);
   return student;
}
3.请求地址:http://192.168.2.23:8080/addstudent?username=liudehua&age=60
username和age必须和Student的类的成员变量名称完全一致
    //通过一个bean来接收,post方式和get方式都可以
@RequestMapping("addstudent")
@ResponseBody
public Object addstudent(Student student) {
    Map<String,Object> map = new HashMap<String,Object>();
    map.put("student",student);
    return map;
}

4.

请求地址:http://192.168.2.23:8080/addstudent/liudehua/60

//通过@PathVariable获取路径中的参数
@RequestMapping("addstudent/{username}/{age}")
@ResponseBody
public Object addstudent(@PathVariable String username,@PathVariable int age) {
    Student student = new Student();
    student.setName(username);
    student.setAge(age);
    
    return student;
}
5.

//用注解@RequestParam绑定请求参数到方法入参
@RequestMapping("addstudent")
@ResponseBody
public Object addstudent(@RequestParam("username") String username,@RequestParam("age") int age) {
    Student student = new Student();
    student.setName(username);
    student.setAge(age);
   return student;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值