SpringBoot RestTemplate POST 요청, from - data 참조

2655 단어 자바springBoot
1. cinfig 설정 클래스
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;
    }

}

좋은 웹페이지 즐겨찾기