小编Jes*_*urn的帖子

spring mvc restcontroller返回json字符串

我有一个Spring MVC控制器,使用以下方法:

@RequestMapping(value = "/stringtest", method = RequestMethod.GET)
public String simpletest() throws Exception {
    return "test";
}
Run Code Online (Sandbox Code Playgroud)

它位于一个控制器内部,如下所示:

@RestController
@RequestMapping(value = "/root")
public class RootController
Run Code Online (Sandbox Code Playgroud)

当我调用其他返回对象的方法时,这些对象被Jackson序列化为JSON.但是这个返回String的方法不会转换为JSON.如果不清楚,这是一个使用curl的例子:

$curl http://localhost:8080/clapi/root/stringtest 
test
Run Code Online (Sandbox Code Playgroud)

所以问题是没有任何引号的"test"不是JSON字符串,但我的REST客户端期望一个字符串.我期望curl命令显示带有引号的字符串,所以它是合法的JSON:

"test"
Run Code Online (Sandbox Code Playgroud)

我正在使用Spring WebMVC 4.1.3和Jackson 2.4.3.我试过在RequestMapping中添加一个"produce"属性,说它应该返回JSON.在这种情况下,发回的Content-Type属性是"application/json",但仍未引用测试字符串.

我可以通过调用JSON库将Java String转换为JSON来解决这个问题,但看起来Spring MVC和Jackson通常会自动执行此操作.但不知何故,他们并没有这样做.我可能配置错误的任何想法只是测试回来而不是"测试"?

json spring-mvc jackson

7
推荐指数
1
解决办法
1万
查看次数

使用TensorFlow for Java进行内存泄漏

以下测试代码泄漏内存:

private static final float[] X = new float[]{1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0};

public void testTensorFlowMemory() {
    // create a graph and session
    try (Graph g = new Graph(); Session s = new Session(g)) {
        // create a placeholder x and a const for the dimension to do a cumulative sum along
        Output x = g.opBuilder("Placeholder", "x").setAttr("dtype", DataType.FLOAT).build().output(0);
        Output dims = g.opBuilder("Const", "dims").setAttr("dtype", DataType.INT32).setAttr("value", Tensor.create(0)).build().output(0);
        Output y = g.opBuilder("Cumsum", "y").addInput(x).addInput(dims).build().output(0);
        // loop a bunch to test memory usage
        for (int i=0; i<10000000; i++){
            // …
Run Code Online (Sandbox Code Playgroud)

java memory-leaks tensorflow

7
推荐指数
1
解决办法
1041
查看次数

标签 统计

jackson ×1

java ×1

json ×1

memory-leaks ×1

spring-mvc ×1

tensorflow ×1