spring mvc 接收json、xml 数据格式,自动解析成对象

来源:互联网 发布:三星clx3305清零软件 编辑:程序博客网 时间:2024/05/22 08:12

1、spring mve 接收json格式数据,

         @RequestMapping(value="/jsonPrase",headers = {"content-type=application/json","content-type=application/xml"})
        @ResponseBody
        public Object addUser(@RequestBody MsgTip tip) {
        
           try {
               System.out.println("add test ..............");
               System.out.println(tip.getMsg() + " == " + tip.getResult());
        } catch (Exception e) {
            e.printStackTrace();
        }
            return new HashMap<String, String>().put("success", "true");
        }
上面红色部分表示,请求的content-type是上面的两个时才能访问该方法,否则会报415

发送的数据:"{\"result\":\"fasle\",\"userId\":null,\"msg\":\"请传入合法的op参数\",\"sightId\":null,\"pathId\":null}"

  MsgTip  字段  

   private String result;
    private Long userId;
    private String msg;
    private Long sightId;
    private Long pathId;



2、spring mvc接收解析xml数据

   与json的差不多   在对应的实体类上加上相应的xml注解,

    @XmlRootElement(name="tips")
   public class MsgTip {

    private String result;
    private Long userId;
    private String msg;
    private Long sightId;
    private Long pathId;

     .......


测试类如下:


public static void testPost(String urlStr) {
        try {
            
            String xmlInfo = getXmlInfo();
            SimpleDateFormat format = new SimpleDateFormat(
                    "yyyy-MM-dd hh:mm:ss");
            System.out.println(format.format(new Date()) + "---发送信息开始。");
            
            System.out.println("xmlInfo=" + xmlInfo);

            URL url = new URL(urlStr);
            URLConnection conn = url.openConnection();
            conn.setRequestProperty("Pragma:", "no-cache");
            conn.setRequestProperty("Cache-Control", "no-cache");
            conn.setRequestProperty("Content-Type", "application/xml"); //如果是json数据 把xml 改为json
            conn.setDoOutput(true);
            OutputStreamWriter out = new OutputStreamWriter(
                    conn.getOutputStream());
            out.write(new String(xmlInfo.getBytes("utf-8")));
            out.flush();
            out.close();

            BufferedReader read = new BufferedReader(new InputStreamReader(
                    conn.getInputStream()));
            String str = read.readLine();
            while (null != str) {
                System.out.println(str);
                str = read.readLine();
            }
            read.close();
            // ReceiveWeixinMsg recemsg = (ReceiveWeixinMsg) new
            // ParseXmlUtil().parseXmlFromObj(ReceiveWeixinMsg.class,
            // conn.getInputStream(), "xml", null);
            // System.out.println("接受信息为:"+recemsg.getContent());
            System.out.println(format.format(new Date()) + "---接受结束。");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }





0 0
原创粉丝点击