Project1 have a POST service, and Project2 use restTemplate.exchange call it, have exception: Request method 'GET' is not supported

Project1 2024-06-17T00:24:04.067+08:00 WARN 6640 --- [rest] [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' is not supported]

Project2 org.springframework.web.client.HttpClientErrorException$MethodNotAllowed: 405 : "{"timestamp":"2024-06-16T16:24:04.102+00:00","status":405,"error":"Method Not Allowed","path":"/list"}" at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:115) at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:183) at org.springframework.web.client

gen project like : https://start.spring.io/#!type=maven-project&language=java&platformVersion=3.3.0&packaging=jar&jvmVersion=17&groupId=com.example&artifactId=rest&name=rest&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.rest&dependencies=web

code:

Project1


@RestController
public class UserRest {

    @RequestMapping(value="list",method=RequestMethod.POST, produces= {MediaType.APPLICATION_JSON_VALUE})
//  @RequestMapping(value="list",method = {RequestMethod.GET, RequestMethod.POST}, produces= {MediaType.APPLICATION_JSON_VALUE})
//  @PostMapping("list")
    public ResponseEntity<String> list(@RequestParam(value="userid",defaultValue="") String userId,
            @RequestParam(value="password",defaultValue="") String password
            ,HttpServletRequest request, HttpServletResponse response
            ) {
        try {
            return ResponseEntity.ok("test");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Project2


import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Enumeration;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@RestController("rest2")
public class MyController2 {

    private RestTemplate restTemplate = new RestTemplate();

    @RequestMapping("/hello2")
    public ResponseEntity<String> hello(@RequestBody(required = false) String body, HttpMethod method,
            HttpServletRequest request, HttpServletResponse response) {

        try {

            HttpHeaders headers = new HttpHeaders();
            Enumeration<String> headerNames = request.getHeaderNames();
            // set head from request
            while(headerNames.hasMoreElements()) {
                String h=headerNames.nextElement();
                headers.set(h, request.getHeader(h));
            }

//          headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity httpEntity = new HttpEntity<>(body, headers);

//          HttpsURLConnection.setFollowRedirects(true);

            HostnameVerifier hostnameVerifier=(hostname,session)->true;

            ClientHttpRequestFactory clientHttpRequestFactory = (new SimpleClientHttpRequestFactory() {

                @Override
                protected void prepareConnection(HttpURLConnection connection , String thhpMethod) throws IOException{
                    if(connection instanceof HttpsURLConnection) {
                        ((HttpsURLConnection) connection).setHostnameVerifier(hostnameVerifier);
                        try {

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });

            ClientHttpRequestFactory factory = new BufferingClientHttpRequestFactory(clientHttpRequestFactory); 

            RestTemplate restTemplate = new RestTemplate(factory);
//          RestTemplate restTemplate = new RestTemplate(); // it is ok
            ResponseEntity<String> serverResponse = null;
//          method = HttpMethod.POST;
            serverResponse = restTemplate.exchange("http://localhost:8080/list", method, httpEntity, String.class); 

         return serverResponse;                                                                         

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.badRequest().build();
    }
}

application.properties spring.application.name=rest2 server.port=8081

Comment From: bclozel

Thanks for getting in touch, but it feels like this is a question that would be better suited to StackOverflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.