RestTemplate 收到的响应不在 json
RestTemplate received response not in json
在 spock 中编写集成测试以供休息 api 使用 JWT 保护时,我遇到了响应内容类型的问题。对于招摇或邮递员来说,没有问题。它只发生在集成测试中。
从测试开始记录:
18:24:57.101 [Test worker] DEBUG org.springframework.web.client.RestTemplate - HTTP POST http://localhost:8080/login
18:24:57.110 [Test worker] DEBUG org.springframework.web.client.RestTemplate - Accept=[text/plain, application/json, application/*+json, */*]
18:24:57.138 [Test worker] DEBUG org.springframework.web.client.RestTemplate - Writing [{login=test, password=test}] as "application/json"
18:24:57.282 [Test worker] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
18:24:57.284 [Test worker] DEBUG org.springframework.web.client.RestTemplate - Reading to [java.lang.String] as "application/json;charset=ISO-8859-1"
这是服务器的响应
<200,###BODY###,[Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers", Authorization:###JWT_TOKEN###, X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY", Content-Type:"application/json;charset=ISO-8859-1", Transfer-Encoding:"chunked", Date:"Sun, 31 Jan 2021 16:58:29 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]>
结果我显然收到了:
Unable to determine the current character, it is not a string, number, array, or object
这是我的测试:
def 'should login player' (){
given:
def credentials = [
"login" : "test",
"password": "test"
]
when:
def response = sendPost(credentials, "login", String.class)
def body = parseResponse(response)
then:
(body.Authentication as String).contains("Bearer")
}
def rest = new RestTemplate()
def headers = new HttpHeaders()
def url = "http://localhost:8080/"
def setup() {
rest.messageConverters.add(new FormHttpMessageConverter())
rest.messageConverters.add(new StringHttpMessageConverter())
headers.setContentType(APPLICATION_JSON)
headers.remove("Accept")
headers.add("Accept", APPLICATION_JSON_VALUE)
}
def sendPost(def body, String endpoint, Class returnType) {
def httpEntity = new HttpEntity<>(body, headers)
rest.postForEntity((url + endpoint), httpEntity, returnType)
}
无法理解为什么在日志中我可以看到 Accept=[text/plain, application/json, application/*+json, /]
如果我只设置 application/json。
即使在 successfulyAuthentication 方法中将 contentType 设置为 json 也无济于事。
知道我做错了什么吗?
抱歉耽搁了。
是的,问题出在 parseResponse 的参数上。我一直在研究整个响应,而不仅仅是 body.
它应该看起来像 def body = parseResponse(response.body)
感谢您花时间调查!!
在 spock 中编写集成测试以供休息 api 使用 JWT 保护时,我遇到了响应内容类型的问题。对于招摇或邮递员来说,没有问题。它只发生在集成测试中。
从测试开始记录:
18:24:57.101 [Test worker] DEBUG org.springframework.web.client.RestTemplate - HTTP POST http://localhost:8080/login
18:24:57.110 [Test worker] DEBUG org.springframework.web.client.RestTemplate - Accept=[text/plain, application/json, application/*+json, */*]
18:24:57.138 [Test worker] DEBUG org.springframework.web.client.RestTemplate - Writing [{login=test, password=test}] as "application/json"
18:24:57.282 [Test worker] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
18:24:57.284 [Test worker] DEBUG org.springframework.web.client.RestTemplate - Reading to [java.lang.String] as "application/json;charset=ISO-8859-1"
这是服务器的响应
<200,###BODY###,[Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers", Authorization:###JWT_TOKEN###, X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY", Content-Type:"application/json;charset=ISO-8859-1", Transfer-Encoding:"chunked", Date:"Sun, 31 Jan 2021 16:58:29 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]>
结果我显然收到了:
Unable to determine the current character, it is not a string, number, array, or object
这是我的测试:
def 'should login player' (){
given:
def credentials = [
"login" : "test",
"password": "test"
]
when:
def response = sendPost(credentials, "login", String.class)
def body = parseResponse(response)
then:
(body.Authentication as String).contains("Bearer")
}
def rest = new RestTemplate()
def headers = new HttpHeaders()
def url = "http://localhost:8080/"
def setup() {
rest.messageConverters.add(new FormHttpMessageConverter())
rest.messageConverters.add(new StringHttpMessageConverter())
headers.setContentType(APPLICATION_JSON)
headers.remove("Accept")
headers.add("Accept", APPLICATION_JSON_VALUE)
}
def sendPost(def body, String endpoint, Class returnType) {
def httpEntity = new HttpEntity<>(body, headers)
rest.postForEntity((url + endpoint), httpEntity, returnType)
}
无法理解为什么在日志中我可以看到 Accept=[text/plain, application/json, application/*+json, /]
如果我只设置 application/json。 即使在 successfulyAuthentication 方法中将 contentType 设置为 json 也无济于事。
知道我做错了什么吗?
抱歉耽搁了。
是的,问题出在 parseResponse 的参数上。我一直在研究整个响应,而不仅仅是 body.
它应该看起来像 def body = parseResponse(response.body)
感谢您花时间调查!!