Spring中的Restful Service在使用postEntity方法时导致400 Bad请求

时间:2018-02-15 06:23:02

标签: java spring-mvc resttemplate jdbctemplate

下面是我在某个类的方法中编写的代码。正好在这行代码ResponseEntity<String> loginResponse = restTemplate.postForEntity(resourseUrl, entity, String.class);我得到400错误的请求错误。

RestTemplate restTemplate = new RestTemplate();
                        String resourseUrl = "https://aspccc.mcompany.in:0000/auth/realms/COMPANY/protocol/openid-connect/token";
                        JSONObject jObj = new JSONObject();
                        jObj.put("Username", "DFG!DJHS$GH");
                        jObj.put("Password", "DFG!DJHS$GH");
                        jObj.put("grant_type", "password");
                        jObj.put("client_id", "COMPANY");

                        HttpHeaders headers = new HttpHeaders();
                        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
                        //headers.add("Content-type", "application/x-www-form-urlencoded");
                        /*headers.add("Authentication", sc.toString());*/
                        HttpEntity<String> entity = new HttpEntity<String>(jObj.toString(), headers);
                        @SuppressWarnings("rawtypes")
                        ResponseEntity<String> loginResponse = restTemplate.postForEntity(resourseUrl, entity, String.class);
                        String body = loginResponse.getBody();
                        System.out.println(body);

以下是我进入编辑器的错误。查找此错误resulted in 400 (Bad Request); invoking error handler

15 Feb 2018 11:35:49,487 WARN : org.springframework.web.client.RestTemplate:568  - POST request for "https://aspccc.mcompany.in:0000/auth/realms/COMPANY/protocol/openid-connect/token" resulted in 400 (Bad Request); invoking error handler
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
    at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:576)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:532)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:489)
    at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:344)
    at com.trinity.displayEtaScheduler.DisplayETAScheduler.insertPanicAlertData(DisplayETAScheduler.java:119)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:833)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)

0 个答案:

没有答案