在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/Java/ 用SpringMVC寫(xiě)APP接口的時(shí)候報(bào)404

用SpringMVC寫(xiě)APP接口的時(shí)候報(bào)404

Controller代碼如下:

@RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public AppResult register(
            HttpServletRequest request,//只要加上這句話(huà)就報(bào)404,所有接口都報(bào),去掉就正常
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            @RequestParam(value = "email", required = false) String email,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "avatar", required = false) MultipartFile avatar) {
        if (!Pattern.matches(USERNAME_REG, username)) {
            return AppResult.createErrorResult("用戶(hù)名格式不正確!");
        }
        if (password.length() < MIN_PASSWORD_LENGTH || password.length() > MAX_PASSWORD_LENGTH) {
            return AppResult.createErrorResult("密碼長(zhǎng)度不正確!");
        }
        if (isExistUser(username)) {
            return AppResult.createErrorResult("用戶(hù)已存在!");
        }

我只要加上HttpServletRequest request這個(gè)參數(shù)就會(huì)報(bào)404,去掉就正常,不知道是什么原因,是不是因?yàn)榕渲玫膯?wèn)題呢?
spring-mvc配置:

 <!-- 自動(dòng)掃描  @Controller-->
    <context:component-scan base-package="com.samoy.sealink.controller">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>

    <!--避免IE執(zhí)行AJAX時(shí),返回JSON出現(xiàn)下載文件 -->
    <bean id="mappingJacksonHttpMessageConverter"
          class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <!-- 啟動(dòng)SpringMVC的注解功能,完成請(qǐng)求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter"/> <!-- JSON轉(zhuǎn)換器 -->
            </list>
        </property>
    </bean>


    <!-- 定義跳轉(zhuǎn)的文件的前后綴 ,視圖模式配置 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <mvc:resources location="/files/" mapping="/file/**"/>
    <!-- 文件上傳配置 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 默認(rèn)編碼 -->
        <property name="defaultEncoding" value="UTF-8"/>
        <!-- 上傳文件大小限制為31M,31*1024*1024 -->
        <property name="maxUploadSize" value="32505856"/>
        <!-- 內(nèi)存中的最大值 -->
        <property name="maxInMemorySize" value="4096"/>
    </bean>

web-xml配置:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mybatis.xml</param-value>
    </context-param>

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>

    <!-- 編碼過(guò)濾器 -->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- spring監(jiān)聽(tīng)器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 防止spring內(nèi)存溢出監(jiān)聽(tīng)器,比如quartz -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>


    <!-- spring mvc servlet-->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <!-- 此處也可以配置成 *.do 形式 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>

    <!-- session配置 -->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>

由于我是app開(kāi)發(fā),對(duì)后端也是一知半解,所以前各位大神不吝賜教,多謝啦!

回答
編輯回答
懷中人

問(wèn)題已解決,在spring-mvc.xml中添加如下配置即可,但具體原理尚不清楚:

<!-- 默認(rèn)的注解映射的支持 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="false">
            <bean id="fastJsonHttpMessageConverter"
                  class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
                <property name="features">
                    <list>
                        <value>WriteDateUseDateFormat</value>
                        <value>QuoteFieldNames</value>
                        <value>WriteMapNullValue</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
2017年9月25日 12:46