-1

我使用 url 直接在浏览器中请求控制器,但我得到'{}' an empty object 。我确定控制器的返回不为空,但页面得到一个空响应。

我还使用工作项目中的工作配置文件构建了一个新项目,但是在新项目中,请求后仍然得到空响应。

我不知道原因,工作项目的任何事情都是一样的。

代码如下。

控制器,只是一个简单的测试

  @Controller
  @RequestMapping("/layout")  
  public class LayoutController {

    @Autowired
    LayoutServer layoutServer;

    @RequestMapping("/test1")
    @ResponseBody
    public ResultUtil test(){
        ResultUtil resultUtil=ResultUtil.success(200,"this is data","this is page data");
        System.out.println(resultUtil);
        return resultUtil;
    }
  }

请求结果,抱歉上传图片失败。它只是得到'{}'。 控制台打印工作正常

配置跟随。

spring-mvc.xml

<?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"
   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">

<!--启用spring的一些annotation -->
<context:annotation-config/>
<!--注册驱动-->
<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <!-- 配置Fastjson支持 -->
        <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>application/json;charset=UTF-8</value>
                </list>
            </property>
            <property name="features">
                <list>
                    <value>WriteDateUseDateFormat</value>
                    <value>QuoteFieldNames</value>
                </list>
            </property>
        </bean>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list><value>text/html;charset=UTF-8</value></list>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

<context:component-scan base-package="com.drawdemo.controller">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="utf-8"/>
    <property name="maxUploadSize" value="10485760000"/>
    <property name="maxInMemorySize" value="40960"/>
</bean>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     version="3.0">

<display-name>Archetype Created Web Application</display-name>

<!-- Spring字符集过滤器 -->
<filter>
  <filter-name>SpringEncodingFilter</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>
  <init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>SpringEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

<!--spring和mybatis的配置文件-->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:/config/spring-mybatis.xml</param-value>
</context-param>

<!--spring监听器-->
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--防止spring内存溢出监听器-->
<listener>
  <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<!-- springMVC核心配置 -->
<servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet- 
class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      classpath:config/spring-mvc.xml
    </param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

4

1 回答 1

0

我得到了答案。

ResultUtil(响应数据)没有getter/setter。花了一天时间,fxxxxxxxk。

一开始,我在控制台上打印ResultUtil,它显示自然,所以我认为它自然并检查配置。

不久前,我再次检查了所有带有错误项目和工作项目的文件,注意到没有 getter/setter,添加并运行,它正在工作。

于 2019-01-07T09:34:09.760 回答