Autowired 注入model对象,在方法中设置对象属性之后,返回之后会报错。

新手上路,请多包涵

@Controller
@RequestMapping ("/andy")
public class helloContoller {

@Autowired
private PayService payService;
@Autowired
private Resource resource;
@Autowired
private Person person;


@RequestMapping ("/hello2")
@ResponseBody
public Person hello2(){
    person.setAge("132");
    person.setName("andy");
    return person;
}

}

返回会报:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.context.expression.StandardBeanExpressionResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.example.controller.Resource$$EnhancerBySpringCGLIB$$397134d0["$$beanFactory"]->org.springframework.beans.factory.support.DefaultListableBeanFactory["beanExpressionResolver"])

阅读 2.8k
1 个回答
public class MyMapper extends ObjectMapper{ 
    public CustomMapper() {
        this.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        // 设置 SerializationFeature.FAIL_ON_EMPTY_BEANS 为 false
        this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    }
}
springmvc配置
<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>application/json; charset=UTF-8</value>
                    <value>application/x-www-form-urlencoded; charset=UTF-8</value>
                </list>
            </property>
            <property name="objectMapper">
                <bean class="MyMapper全限定名">
                </bean>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题