Spring - Return 将字符串或 JSONObject 转换为 JSONP
Spring - Return String or JSONObject into JSONP
我能够 return 来自自定义 java 对象的 JSONP 没有问题(如下:http://www.concretepage.com/spring-4/spring-4-mvc-jsonp-example-with-rest-responsebody-responseentity),但是当我尝试 return 字符串时使用 JSONP,包装功能消失了
我在做什么:
@RequestMapping(value ="/book", produces = {MediaType.APPLICATION_JSON_VALUE, "application/javascript"})
public @ResponseBody ResponseEntity<String> bookInfo() {
JSONObject test = new JSONObject();
test.put("uno", "uno");
return new ResponseEntity<String>(test.toString(), HttpStatus.OK);
}
调用服务:
http://<server>:port//book?callback=test
Returns:
{"uno":"uno"}
预期结果:
test({"uno":"uno"})
也尝试直接 return JSONObject ResponseEntity.accepted().body(test);
但我收到 406 错误。有什么想法吗?
错误看起来像来自 this example 的 class JsonpAdvice
不适用于请求映射。
@ControllerAdvice
public class JsonpAdvice extends AbstractJsonpResponseBodyAdvice {
public JsonpAdvice() {
super("callback");
}
}
我使用了 HashMap,因为它在这里有类似的用途,而且 HashMap 在这个例子中使用起来更直接:
@RequestMapping(value="/book", produces=MediaType.APPLICATION_JSON)
public ResponseEntity<Map> bookInfo() {
Map test = new HashMap();
test.put("uno", "uno");
return ResponseEntity.accepted().body(test);
}
这为我提供了结果:
// http://localhost:8080/book?callback=test
/**/test({
"uno": "uno"
});
我正在使用 Spring 启动:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
我能够 return 来自自定义 java 对象的 JSONP 没有问题(如下:http://www.concretepage.com/spring-4/spring-4-mvc-jsonp-example-with-rest-responsebody-responseentity),但是当我尝试 return 字符串时使用 JSONP,包装功能消失了
我在做什么:
@RequestMapping(value ="/book", produces = {MediaType.APPLICATION_JSON_VALUE, "application/javascript"})
public @ResponseBody ResponseEntity<String> bookInfo() {
JSONObject test = new JSONObject();
test.put("uno", "uno");
return new ResponseEntity<String>(test.toString(), HttpStatus.OK);
}
调用服务:
http://<server>:port//book?callback=test
Returns:
{"uno":"uno"}
预期结果:
test({"uno":"uno"})
也尝试直接 return JSONObject ResponseEntity.accepted().body(test);
但我收到 406 错误。有什么想法吗?
错误看起来像来自 this example 的 class JsonpAdvice
不适用于请求映射。
@ControllerAdvice
public class JsonpAdvice extends AbstractJsonpResponseBodyAdvice {
public JsonpAdvice() {
super("callback");
}
}
我使用了 HashMap,因为它在这里有类似的用途,而且 HashMap 在这个例子中使用起来更直接:
@RequestMapping(value="/book", produces=MediaType.APPLICATION_JSON)
public ResponseEntity<Map> bookInfo() {
Map test = new HashMap();
test.put("uno", "uno");
return ResponseEntity.accepted().body(test);
}
这为我提供了结果:
// http://localhost:8080/book?callback=test
/**/test({
"uno": "uno"
});
我正在使用 Spring 启动:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
</dependencies>