zoukankan      html  css  js  c++  java
  • taotao服务测试http请求需要返回json时出现406错误处理

        @Test
        public void doPost() throws Exception {
            
            CloseableHttpClient httpClient = HttpClients.createDefault();
    //        HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.html");
    // 注意:如果请求的url后缀是 .html则浏览器不能返回正确的json数据,会返回406错误,所以需要修改请求url的后缀为其他    
            HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.action");
            CloseableHttpResponse response = httpClient.execute(post);
            HttpEntity entity = response.getEntity();
            String string = EntityUtils.toString(entity, "utf-8");
            System.out.println(string);
            response.close();
            httpClient.close();
        }

    项目的web.xml中最后加入

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>taotao-portal</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      
      <!-- 加载spring容器 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/applicationContext-*.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!-- 解决post乱码 -->
        <filter>
            <filter-name>CharacterEncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>utf-8</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>CharacterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <!-- springmvc的前端控制器 -->
        <servlet>
            <servlet-name>taotao-portal</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring/springmvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <!-- 伪静态化 -->
        <servlet-mapping>
            <servlet-name>taotao-portal</servlet-name>
            <url-pattern>*.html</url-pattern>
        </servlet-mapping>
        
        <!-- 配置需要返回json类型请求的拦截 -->
        <servlet-mapping>
            <servlet-name>taotao-portal</servlet-name>
            <url-pattern>*.action</url-pattern>
        </servlet-mapping>
    </web-app>
  • 相关阅读:
    linux 硬盘满了 怎么优化
    Mysql 忘记密码 Linux
    嵌入式新闻早班车-第18期
    《安富莱嵌入式周报》第225期:2021.08.09--2021.08.15
    【DSP教程】第43章 IIR滤波器的Matlab设计
    【DSP教程】第42章 IIR无限冲击响应滤波器设计
    嵌入式新闻早班车-第17期
    《安富莱嵌入式周报》第224期:2021.08.02--2021.08.08
    H7-TOOL重大更新,发布WiFi版,新增暗黑主题,脱机烧录增加大唐半导体,自此高速USB,以太网和WiFi方式全部打通(2021-08-07)
    【STM32H7的DSP教程】第41章 FIR滤波器的群延迟(重要)
  • 原文地址:https://www.cnblogs.com/libin6505/p/9759064.html
Copyright © 2011-2022 走看看