• springmvc 开涛 生产者/消费者


    媒体类型:

    text/html, text/plain, text/xml
    image/gif, image/jpg, image/png
    application/x-www-form-urlencodeed, application/json, application/xml  有q参数,q参数为媒体类型的质量因子,越大则优先权越高(从0到1)
    multipart/form-data
    

    内容类型:Content-Type:MIME  有charset参数

    服务器端有header, 客户端有Accept,扩展名,参数-ContentNegotiatingViewResolver(简单方法)

    spring3.1通过consume和product来限定

    窄化时是覆盖而非继承,其他(header,param)是继承

    服务器端header详解

    1、form get

    @RequestMapping("/content")
    public ModelAndView showForm(@ModelAttribute("user") User user){
    	ModelAndView mv = new ModelAndView();		
    	mv.setViewName("hello");
    	return mv;
    }
    

    2、form post x

    @RequestMapping(value="/content", method=RequestMethod.POST, headers="Content-Type=application/x-www-form-urlencoded")
    public ModelAndView request1(HttpServletRequest request){		
    	//System.out.println("/content post");
    	
    	String contentType=request.getContentType();
    	System.out.println("contentType:"+contentType);
    	
    	String characterEncoding=request.getCharacterEncoding();
    	System.out.println("characterEncoding:"+characterEncoding);		
    	
    	System.out.println(request.getParameter("c"));
    	
    	ModelAndView mv = new ModelAndView();		
    	mv.setViewName("success");
    	return mv;
    }
    

    3、form post json

    @RequestMapping(value="/content", method=RequestMethod.POST, headers="Content-Type=application/json")
    public void request2(HttpServletRequest request, HttpServletResponse response) throws IOException{
    	System.out.println("/json content post");
    	System.out.println("charset:"+request.getCharacterEncoding());
    	
    	InputStream is = request.getInputStream();
    	byte bytes[] = new byte[request.getContentLength()];
    	is.read(bytes);		
    	
    	String jsonStr = new String(bytes, request.getCharacterEncoding());
    	System.out.println("json data:"+jsonStr);		
    	
    	ModelAndView mv = new ModelAndView();		
    	mv.setViewName("success");
    	return mv;	
    	
    }
    

    4、form post x ClientHttpRequest

    @RequestMapping(value="/content", method=RequestMethod.POST, headers="Content-Type=application/x-www-form-urlencoded")
    public void request(HttpServletResponse response) throws IOException, URISyntaxException{
    	System.out.println("/x content post");
    	
    	String url = "http://localhost:8080/springmvc/content";
    	ClientHttpRequest request = new SimpleClientHttpRequestFactory().createRequest(new URI(url), HttpMethod.POST);
    	request.getHeaders().set("Content-Type", "application/json;charset=gbk");
    	String jsonData = "{"username":"zhang"}";
    	request.getBody().write(jsonData.getBytes("gbk"));
    	request.execute();
    	System.out.println("status:"+response.getStatus());	
    	
    	response.setContentType("text/html;charset=utf-8");
    	response.getWriter().write("json response");	
    	
    }
    

    1-2正常情况

    1-3没法设置json,所以1-4-3-4

    2注释掉,因为get想直接到post x

    ClientHttpRequest与真正的HttpServletResponse不同,有些方法不太一样。

    客户端Accept

    不太了解,client的方法

    private static void jsonRequest() throws IOException, URISyntaxException {
        //请求的地址
        String url = "http://localhost:9080/springmvc-chapter6/response/ContentType";
        //①创建Http Request(内部使用HttpURLConnection)
        ClientHttpRequest request = 
            new SimpleClientHttpRequestFactory().   
                createRequest(new URI(url), HttpMethod.POST);
        //②设置客户端可接受的媒体类型(即需要什么类型的响应体数据)
        request.getHeaders().set("Accept", "application/json");        
        //③发送请求并得到响应
        ClientHttpResponse response = request.execute();
        //④得到响应体的编码方式
        Charset charset = response.getHeaders().getContentType().getCharSet();        
        //⑤得到响应体的内容        
        InputStream is = response.getBody();
        byte bytes[] = new byte[(int)response.getHeaders().getContentLength()];
        is.read(bytes);
        String jsonData = new String(bytes, charset);
        System.out.println("charset : " + charset + ", json data : " + jsonData);
    }
    

    spring3.1

    需要添加的bean

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
    
    consumes = {application/json};
    produces = {application/json}
    

    总结:有些方法不太熟练

    HttpServletRequest req
    req.getContentType();
    req.getCharacterEncoding();
    req.getParameter("c");  //form name c
    req.getContentLength();
    
    HttpServletResponse res
    res.setContentType("text/html;charset=utf-8");
    res.getWriter().write("json response");
    
    ClientHttpRequest req
    req.getHeaders().set("Content-Type", "application/json;charset=gbk");
    req.getBody().write(jsonData.getBytes("gbk"));
    req.execute();
    

      

  • 相关阅读:
    创始人透露Twitter新盈利计划:第三方将受益
    试试ScribeFire转发我的其它博客
    2009年12月30日:新网因清除URL转发域名导致DNS解析故障
    VS2008 如果更改源代码管理插件,将关闭活动解决方案或项目
    Linq使用Group By经验总结
    使用C#的BitmapData
    Java与C#开发上的一些差异与转换方法
    成都七中成绩文件导入SQL脚本
    用SQL SERVER对EXCEL数据进行处理
    NULLIF和ISNULL
  • 原文地址:https://www.cnblogs.com/wang-jing/p/4574161.html
Copyright © 2020-2023  润新知