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

鍍金池/ 問答/Java/ Autowired 注入model對象,在方法中設(shè)置對象屬性之后,返回之后會報(bào)錯(cuò)

Autowired 注入model對象,在方法中設(shè)置對象屬性之后,返回之后會報(bào)錯(cuò)。

@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;
}

}

返回會報(bào):
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"])

回答
編輯回答
莫小染
public class MyMapper extends ObjectMapper{ 
    public CustomMapper() {
        this.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        // 設(shè)置 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>
2018年8月25日 13:56