使用Play Framework Java设置内容类型标头

时间:2014-03-23 16:17:07

标签: playframework playframework-2.0

我正在使用Play Framework 2.2.1和Java。我正在尝试测试我的控制器,这样做我需要将Content-Type标头设置为“application / json”。我在我的FakeRequest上尝试使用'withHeader(“Content-Type”,“application / json”)和'withJsonBody'。每当我使用这些方法中的任何一个时,似乎我得到一个空指针异常。关于我是如何设置标题的任何想法?

FakeRequest fr = new FakeRequest("PUT", "/create");
fr.withHeader(CONTENT_TYPE, "application/json");

//I have tried this in place of calling withHeader as above
//JsonNode arg0 = Json.parse("{\"test\":1}");
//fr.withJsonBody(arg0);


 try {
     Result result = Helpers.route(fr);
     assertThat(result).isNotNull();
     assertThat(status(result)).isEqualTo(CREATED);
 }catch(Throwable t) {
     t.printStackTrace();
 }

这是我得到的堆栈跟踪:

java.lang.NullPointerException
    at play.test.Helpers.unwrapJavaResult(Helpers.java:61)
    at play.test.Helpers.header(Helpers.java:220)
    at play.test.Helpers.charset(Helpers.java:254)
    at play.test.Helpers.contentAsString(Helpers.java:289)
    at controllers.GroupControllerTest$10.run(GroupControllerTest.java:233)
    at play.test.Helpers.running(Helpers.java:376)
    at controllers.GroupControllerTest.callPutFacetForGroupAdminAsAdmin(GroupControllerTest.java:216)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runners.Suite.runChild(Suite.java:127)
    at org.junit.runners.Suite.runChild(Suite.java:26)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:138)
    at com.novocode.junit.JUnitRunner.run(JUnitRunner.java:90)
    at sbt.RunnerWrapper$1.runRunner2(FrameworkWrapper.java:220)
    at sbt.RunnerWrapper$1.execute(FrameworkWrapper.java:233)
    at sbt.ForkMain$Run.runTest(ForkMain.java:239)
    at sbt.ForkMain$Run.runTestSafe(ForkMain.java:211)
    at sbt.ForkMain$Run.runTests(ForkMain.java:187)
    at sbt.ForkMain$Run.run(ForkMain.java:251)
    at sbt.ForkMain.main(ForkMain.java:97)

1 个答案:

答案 0 :(得分:1)

我是这样做的

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<nav>
 <button type="button" onclick="go()">Actual plant functional type</button> 
</nav>

<section>
<iframe  id="test" style="border: none;" height="494" width="1100"  src="http://localhost:8080/geoexplorer/viewer/#maps/1"></iframe>
</section>

<script type="text/javascript">
function go() {
    document.getElementById("test").src = "http://localhost:8080/geoexplorer/viewer/#maps/2";
}
</script>

</body>
</html>