Get请求报错406

Get请求报错406

com.imooc.reader.controllercom.imooc.reader.entity.Categorycom.imooc.reader.service.CategoryServicecom.imooc.reader.utils.ResponseUtilsorg.springframework.stereotype.org.springframework.web.bind.annotation.org.springframework.web.bind.annotation.org.springframework.web.bind.annotation.javax.annotation.java.util.List()
CategoryController {
    CategoryService ()
    ResponseUtils () {
        ResponseUtils resp = {
            List<Category> categories = .selectAll()resp = ResponseUtils().put(categories)} (Exception e) {
            e.printStackTrace()resp = ResponseUtils(e.getClass().getSimpleName()e.getMessage())}
        resp}
}

问题描述:

测试报错406

相关截图:

https://img1.sycdn.imooc.com//climg/63f4eac409339fea07710291.jpg

相关代码:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/util
           https://www.springframework.org/schema/util/spring-util.xsd">
    <context:component-scan base-package="com.imooc.reader"></context:component-scan>
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=utf-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <mvc:default-servlet-handler/>
    <!--配置数据源(连接那个数据库)-->
    <bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url"
                  value="jdbc:mysql://localhost:3306/imooc_reader?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai&amp;allowPublicKeyRetrieval=true"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
        <property name="initialSize" value="3"/>
        <property name="maxActive" value="10"/>
    </bean>
    <!--配置sessionFactory-->
<!--    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">-->
<!--    更改配置mybatis-plus初始化-->
        <bean id="sessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dateSource"/>
        <property name="mapperLocations" value="classpath:mappers/*.xml"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--            Mybatis-plus 3.4以后版本需要在MyBatisSqlSessionFactory屬性中進行設置-->
        <property name="plugins">
            <array>
                <bean class="com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor">
                    <property name="interceptors">
                        <list>
                            <bean class="com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor"/>
                        </list>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

    <!--配置mapper扫描器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.imooc.reader.mapper"/>
    </bean>
    <bean id="transactionManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dateSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManger"/>
</beans>


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,

1、HTTP 406 错误指无法接受 (Not acceptable)错误。根据上述反馈代码(已乱码)老师这边无法准确查看,这里建议同学检查注解是否缺少

https://img1.sycdn.imooc.com//climg/63f57b5c09b139d805770250.jpg

2、检查fastjson相关依赖是否正确引入,例如:

https://img1.sycdn.imooc.com//climg/63f57bec099598b616160465.jpg

3、下载课程源码,对比各个类与xml文件的区别,将自己的代码替换至源代码中试着运行,看是否出现相同问题

祝学习愉快~

  • package com.imooc.reader.controller;
    import com.imooc.reader.entity.Category;
    import com.imooc.reader.service.CategoryService;
    import com.imooc.reader.utils.ResponseUtils;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    import java.util.List;
    
    @RestController
    @RequestMapping("/api/category")
    public class CategoryController {
        @Resource
        private CategoryService categoryService;
    
        @GetMapping("/list")
        public ResponseUtils list() {
            //标准结构 code = "0"
            ResponseUtils resp = null;
            try {
                List<Category> categories = categoryService.selectAll();
                resp = new ResponseUtils().put("list",categories);
            } catch (Exception e) {
                e.printStackTrace();
                resp = new ResponseUtils(e.getClass().getSimpleName(), e.getMessage());
            }
            return resp;
        }
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:task="http://www.springframework.org/schema/task"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/task
                http://www.springframework.org/schema/task/spring-task.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx.xsd">
        <context:component-scan base-package="com.imooc.reader"></context:component-scan>
        <mvc:annotation-driven>
            <mvc:message-converters>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>application/json;charset=utf-8</value>
                        </list>
                    </property>
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>
        <mvc:default-servlet-handler/>
        <!--配置数据源(连接那个数据库)-->
        <bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
            <property name="url"
                      value="jdbc:mysql://localhost:3306/imooc_reader?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai&amp;allowPublicKeyRetrieval=true"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
            <property name="initialSize" value="3"/>
            <property name="maxActive" value="10"/>
        </bean>
        <!--配置sessionFactory-->
    <!--    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">-->
    <!--    更改配置mybatis-plus初始化-->
            <bean id="sessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
            <property name="dataSource" ref="dateSource"/>
            <property name="mapperLocations" value="classpath:mappers/*.xml"/>
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
    <!--            Mybatis-plus 3.4以后版本需要在MyBatisSqlSessionFactory屬性中進行設置-->
            <property name="plugins">
                <array>
                    <bean class="com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor">
                        <property name="interceptors">
                            <list>
                                <bean class="com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor"/>
                            </list>
                        </property>
                    </bean>
                </array>
            </property>
        </bean>
    
        <!--配置mapper扫描器-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.imooc.reader.mapper"/>
        </bean>
        <bean id="transactionManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dateSource"/>
        </bean>
        <tx:annotation-driven transaction-manager="transactionManger"/>
    </beans>
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE  configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org./dtd/mybatis-3-config.dtd">
    <configuration>
        <settings>
            <setting name="mapUnderscoreToCamelCase" value="true"/>
        </settings>
    <!--    MyBatis-plus 3.4版本以前配置分頁插件-->
    <!--    <plugins>-->
    <!--        <plugin interceptor="com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor"></plugin>-->
    <!--    </plugins>-->
    
    </configuration>


    2023-02-22 22:22:56
  • 同学你好,拼接测试上述贴出代码没有任何问题,如下所示:

    https://img1.sycdn.imooc.com//climg/63f6cacb092308ad09450159.jpg

    这里同学是否按照之前给出的第二点进行排查呢?关于jar包的引入是否正确加载完毕呢?

    祝学习愉快~

    2023-02-23 10:11:40
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师