Spring项目用restTemplate访问接口获取数据

import org.springframework.web.client.RestTemplate;

@RequestMapping("/rest/basis/competition/brand")
@Controller
public class CompetitonBrandRestController {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 
     * @param queryDto
     * @return
     */
    @JsonResponseBody(message = "分页查询")
    @RequestMapping(path = "/daily/query", method = RequestMethod.POST, produces = {"application/json"})
    public PageVo queryDaily(@RequestBody CompetitionSalesDTO queryDto) {
        PageVo vo = (PageVo) map2Object((Map) queryCompetition(dailyUrl, queryDto).get("data"), PageVo.class);
        return vo;
    }

    private Map queryCompetition(String url, Object queryDto) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        Map params = bean2Map(queryDto);
        HttpEntity> requestEntity = new HttpEntity<>(params, headers);
        ResponseEntity exchange = restTemplate.exchange(url, HttpMethod.POST, requestEntity, Map.class);
        Map body = (Map) exchange.getBody();
        return body;
    }

}

 Javabean和Map相互转换的方法写在另外一篇。

 

你可能感兴趣的:(Java)