这是我的javascript:
function getWeather() {
$.getJSON('getTemperature/' + $('.data option:selected').val(), null, function(data) {
alert('Success');
});
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
@RequestMapping(value="/getTemperature/{id}", headers="Accept=*/*", method = RequestMethod.GET)
@ResponseBody
public Weather getTemparature(@PathVariable("id") Integer id){
Weather weather = weatherService.getCurrentWeather(id);
return weather;
}
Run Code Online (Sandbox Code Playgroud)
为spring-servlet.xml
<context:annotation-config />
<tx:annotation-driven />
Run Code Online (Sandbox Code Playgroud)
得到此错误:
GET http://localhost:8080/web/getTemperature/2 406 (Not Acceptable)
Run Code Online (Sandbox Code Playgroud)
头:
响应标题
Server Apache-Coyote/1.1
Content-Type text/html;charset=utf-8
Content-Length 1070
Date Sun, 18 Sep 2011 17:00:35 GMT
Run Code Online (Sandbox Code Playgroud)
请求标题
Host localhost:8080
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
Accept application/json, text/javascript, */*; q=0.01
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, …Run Code Online (Sandbox Code Playgroud) 我目前正在使用 Spring boot test 测试我的一项服务。该服务导出所有用户数据并在成功完成后生成 CSV 或 PDF。在浏览器中下载文件。
以下是我在测试类中编写的代码
MvcResult result = MockMvc.perform(post("/api/user-accounts/export").param("query","id=='123'")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_PDF_VALUE)
.content(TestUtil.convertObjectToJsonBytes(userObjectDTO)))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_PDF_VALUE))
.andReturn();
String content = result.getResponse().getContentAsString(); // verify the response string.
Run Code Online (Sandbox Code Playgroud)
下面是我的资源类代码(调用到这个地方)-
@PostMapping("/user-accounts/export")
@Timed
public ResponseEntity<byte[]> exportAllUsers(@RequestParam Optional<String> query, @ApiParam Pageable pageable,
@RequestBody UserObjectDTO userObjectDTO) {
HttpHeaders headers = new HttpHeaders();
.
.
.
return new ResponseEntity<>(outputContents, headers, HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)
当我调试我的服务并在退出之前放置调试时,我得到的内容类型为“应用程序/pdf”,状态为 200。我试图在我的测试用例中复制相同的内容类型。不知何故,它在执行过程中总是抛出以下错误 -
java.lang.AssertionError: Status
Expected :200
Actual :406
Run Code Online (Sandbox Code Playgroud)
我想知道,我应该如何检查我的响应(ResponseEntity)。还有什么应该是响应所需的内容类型。