SpringBoot RestTemplate POST 요청, from - data 참조
2655 단어 자바springBoot
package com.haoqian.crm.kscrm.configration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
/**
* @ClassName RestTemplateConfig
* Created by ***** 2018/11/16 13:01
**/
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(5000);//ms
factory.setConnectTimeout(15000);//ms
return factory;
}
}
2. 시작 클래스 에 주석 추가
@EnableAutoConfiguration
3. 실례
import com.haoqian.crm.kscrm.dto.LoginInfoDTO;
import com.haoqian.crm.kscrm.utils.ResultVOUtil;
import com.haoqian.crm.kscrm.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* @ClassName AccountController
* Created by **** 2018/11/2 15:29
**/
@RestController
public class AccountController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/check/token")
public LoginInfoDTO checkToken(String checkToken,HttpServletRequest request){
System.out.println(checkToken);
String url = "http://**.com/api/cas/authenticate";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap map = new LinkedMultiValueMap();
map.add("checkToken",checkToken);
HttpEntity requestBody = new HttpEntity(map, headers);
ResponseEntity responseEntity = restTemplate.postForEntity(url, requestBody, LoginInfoDTO.class);
LoginInfoDTO loginInfoDTO = responseEntity.getBody();
return loginInfoDTO;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.