Spring請求參數(參數對象的屬性對象)converter


問題描述:請求參數address,接受請求參數用people接受(address是個對象,也是people的一個屬性)

一、entity類創建 (people、address)

@Data
public
class People { private String name; private Date sourcedate; private Address address; }
public class Address {
    
    private String homeAddress;
}

二、類型轉換器的 AddressConverter

public class AddressConverter implements Converter<String,Address>{


    @Override
    public Address convert(String source) {
        return new Address(source);
    }

}

三、將類型轉換器添加到spring中

@Configuration
public class WebConfiguration {
    
    @Bean
    public GenericConversionService getGenericConversionService(@Autowired GenericConversionService conversionService) {
        conversionService.addConverter(new AddressConverter());
        System.out.println("類型轉換已加入!");
        return conversionService;
        
    }
}

四、controller

@Controller
public class HelloController {
    @RequestMapping("/test")
    @ResponseBody
    public String hello( People people) {
        
        return "===="+people.getAddress();
    }
}

五、測試


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM