spring - Access-Control-Expose-Headers Configuration for Custom Response Headers AngularJS -
i'm implementing custom response headers on resource, after reading on similar question, added 'access-control-expose-headers' cors headers, must not have configured correctly.
here headers receive on request of resource: receive 'response-header' of "steve", should allowed 'access-control-expose-headers'
access-control-allow-headers:x-requested-with, request-header, response-header access-control-allow-methods:post, get, options, delete access-control-allow-origin:* access-control-expose-headers:response-header response-header:steve
but, angular still not have access custom header. custom request-header, not custom response-header:
{ "data": { "id": 2, "content": "hello, frank!" }, "status": 200, "config": { "method": "get", "transformrequest": [ null ], "transformresponse": [ null ], "url": "http:\/\/localhost:8080\/greeting", "headers": { "accept": "application\/json, text\/plain, *\/*", "request-header": "frank" } }, "statustext": "ok" }
angular 1.3 $httpprovider.interceptor:
'use strict'; angular .module('headerdemouiapp') .factory('authinterceptor', function authinterceptor(nameservice) { return { request: handlerequest, response: handleresponse }; function handlerequest(config) { if (angular.isdefined(config.headers)) { config.headers['request-header'] = "frank"; } return config; } function handleresponse(response) { console.log('response: ' + json.stringify(response)); if (angular.isdefined(response.config.headers['response-header'])) { var resphead = response.config.headers['response-header']; console.log('response-header: ' + resphead); nameservice.responsename=resphead; } return response; } });
and, spring version 4.1.6 @restcontroller adding header.
@restcontroller public class greetingcontroller { private static final string template = "hello, %s!"; private final atomiclong counter = new atomiclong(); @requestmapping("/greeting") public greeting greeting( @requestparam(value = "name", defaultvalue = "world") string name, httpservletresponse response, @requestheader("request-header") string headername) { // sets our custom response header response.setheader("response-header","steve"); return new greeting(counter.incrementandget(), string.format(template, headername)); } }
thanks taking look!
issue api greetingcontroller.java
change greeting method return responseentity , use httpheaders, below
@requestmapping("/greeting") public responseentity<greeting> greeting(@requestparam(value = "name", defaultvalue = "world") string name, @requestheader("request-header") string headername) { multivaluemap<string, string> headers = new httpheaders(); // sets our custom response header headers.add("response-header","steve"); return new responseentity<greeting>(new greeting(counter.incrementandget(),string.format(template, headername)), headers, httpstatus.ok); }
below headers response postman client
content-length → 2 content-type → text/plain;charset=utf-8 date → wed, 27 may 2015 15:53:47 gmt response-header → steve server → apache-coyote/1.1
Comments
Post a Comment