博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springMVC学习(11)-json数据交互和RESTful支持
阅读量:6998 次
发布时间:2019-06-27

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

一、json数据交互:

json数据格式在接口调用中、html页面中较常用,json格式比较简单,解析还比较方便。

比如:webservice接口,传输json数据.

springMVC进行json交互

1)环境准备:

加载json转换的jar包:

springmvc中使用jackson的包进行json转换(@requestBody和@responseBody使用下边的包进行json转)

jackson-core-asl-1.9.11.jar

jackson-mapper-asl-1.9.11.jar

2)配置json转换器;

如果是配置单个的注解适配器,要加入下面配置:

1 
2
3
4
5
6
7
8
View Code

如果使用<mvc:annotation-driven /> 则不用定义上边的内容。

3)代码实现:测试:

1 package com.cy.controller; 2  3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestBody; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.ResponseBody; 8  9 import com.cy.po.ItemsCustom;10 import com.cy.service.ItemsService;11 12 /**13  * json交互测试14  *15  */16 @Controller17 public class JsonTest {18     19     @Autowired20     private ItemsService itemsService;21     22     //请求json串(商品信息),输出json(商品信息)23     //@RequestBody将请求的商品信息的json串转成itemsCustom对象24     @RequestMapping("/requestJson")25     @ResponseBody26     public  ItemsCustom requestJson(@RequestBody ItemsCustom itemsCustom) throws Exception{27         int id = itemsCustom.getId();28         ItemsCustom itemsCustom2 = itemsService.findItemsById(id);29         return itemsCustom2;30     }31     32     //请求key/value,输出json33     //@ResponseBody将itemsCustom转成json输出34     @RequestMapping("/responseJson")35     @ResponseBody36     public ItemsCustom responseJson(ItemsCustom itemsCustom) throws Exception{37         int id = itemsCustom.getId();38         ItemsCustom itemsCustom2 = itemsService.findItemsById(id);39         return itemsCustom2;40     }41 }

jsp页面:

1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3  4  5  6 
7 json交互测试 8 9 38 39 40 41 42 43
View Code

测试结果:

数据库内容:

requestJson:

responseJson:

 

 

二、RESTful支持:

 RESTful介绍:

RESTful(即Representational State Transfer的缩写)其实是一个开发理念,是对http的很好的诠释。

1、对url进行规范,写RESTful格式的url

非REST的url:http://...../queryItems.action?id=001&type=T01

REST的url风格:http://..../items/001

特点:url简洁,将参数通过url传到服务端

2、http的方法规范

不管是删除、添加、更新。。使用url是一致的,如果进行删除,需要设置http的方法为delete,同理添加put。。。

后台controller方法:判断http方法,如果是delete执行删除,如果是post执行更新。

3、对http的contentType规范

请求时指定contentType,要json数据,设置成json格式的type。。

 

下面是RESTful的例子:这个例子主要是对url进行了规范:

1)ItemsController:

定义方法,进行url映射使用REST风格的url,将查询商品信息的id传入controller .

@Controller@RequestMapping("/items")public class ItemsController {        @Autowired    private ItemsService itemsService;        //查询商品信息,输出json    ///itemsView/{id}里边的{id}表示占位符,通过@PathVariable获取占位符中的参数,    //如果占位符中的名称和形参名一致,在@PathVariable可以不指定名称    @RequestMapping("/itemsView/{id}")    @ResponseBody    public  ItemsCustom itemsView(@PathVariable("id") Integer itemId)throws Exception{        ItemsCustom itemsCustom = itemsService.findItemsById(itemId);        return itemsCustom;    }                ....}

2)REST风格的前端控制器配置、静态资源文件配置:

由于REST风格是url是不带后缀的,这里配置url-pattern为/,亦需要配置静态文件不让dispatcherServlert解析:

web.xml中配置:

1 
2
3
springmvc_rest
4
org.springframework.web.servlet.DispatcherServlet
5
6
contextConfigLocation
7
classpath:spring/springmvc.xml
8
9
10
11
springmvc_rest
12
/
13
View Code

springmvc中配置访问静态资源:

访问结果:

 

转载于:https://www.cnblogs.com/tenWood/p/6337633.html

你可能感兴趣的文章