RestTemPlate은 클라이언트 측의 웹 서비스에 액세스하는 데 사용되는 클래스입니다. 봄의 다른 템플릿 클래스 (예 : jdbctemplate 및 jmstemplate)와 매우 유사하며 콜백 메소드를 제공하고 httpmessageconverter 클래스를 구성하여 템플릿을 사용자 정의 할 수도 있습니다. 클라이언트 작업은 RestTemplate 및 HttpMessageConveter 클래스를 사용하여 완전히 수행 할 수 있습니다.
1. RestTemplate의 콩을 선언합니다
@BeanPublic resttemplate resttemplate () {return new resttemplate ();}2. 시뮬레이션 호출
@ServicePublic 클래스 smsservice {// resttemplate를 주입 @autowired resttemplate resttemplate; public String sendmsg (문자열 phonenum, 문자열 텍스트) {// set httpheaders headers = new httpheaders (); headers.setContentType (mediaType.Application_form_urlencoded); // 매개 변수 설정 multivaluemap <string, string> p = new LinkedMultivaluemap <> (); p.add ( "사용자 이름", "xxx"); p.add ( "비밀번호", "yyy"); p.add ( "phonenum", phonenum); p.add ( "내용", 텍스트); // 요청 제출 httpentity <multivaluemap <string, string >> entity = new httpentity <multivaluemap <string, String >> (p, 헤더); 문자열 결과 = resttemplate.postforoBject ( "http : // ...", Entity, String.class); 반환 결과; }}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.