2016-09-23 6 views
0

RestTemplate을 사용하여 URL에 POST 요청으로 사용자 지정 개체와 사용자 지정 헤더를 보내려고합니다. 다음은 내가 사용하고있는 코드입니다.Spring의 RestTemplate을 사용하여 사용자 지정 개체 및 헤더를 보내는 방법

MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); 
    headers.add("Authorization: key=", SERVER_KEY); 
    headers.add("Content-Type", "application/json"); 

    RestTemplate restTemplate = new RestTemplate(); 
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); 

    CustomDataModel customDataModel = new CustomDataModel(); 
    customDataModel.setTitle(message); 
    customDataModel.setBody(rowId); 

    CustomNotificationModel customNotificationModel = new CustomNotificationModel(); 
    customNotificationModel.setRegistration_ids(list); 
    customNotificationModel.setPriority("high"); 
    customNotificationModel.setData(customDataModel); 

    HttpEntity<CustomNotificationModel> entity = new HttpEntity<CustomNotificationModel>(customNotificationModel, headers); 

    restTemplate.postForLocation(REST_SERVICE_URI, entity); 

하지만 다음과 같은 오류가 발생합니다.

org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error 
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94) 
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641) 
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597) 
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557) 
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:475) 
at com.humworld.sync.restclient.RestClient.notifyUser(RestClient.java:68) 
at com.humworld.sync.controller.SyncRestController.saveUserDetails(SyncRestController.java:94) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:497) 
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) 
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) 
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) 
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832) 
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743) 
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) 
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961) 
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895) 
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) 
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) 
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) 
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
at java.lang.Thread.run(Thread.java:745) 

변경해야 할 사항을 알려주십시오.

+0

컨트롤러 및 mvc 구성에 대한 자세한 정보를 제공하십시오. 미리 감사드립니다! –

답변

0

죄송합니다. 인증 헤더를 잘못 작성했습니다. 다음과 같이 작성해야합니다.

headers.add("Authorization", "key=" + SERVER_KEY); 

코드가 잘 작동합니다. 다시 한번 죄송합니다.