NutzCN Logo
问答 nutz rest HttpPost
发布于 2089天前 作者 qq_f55c6d47 2006 次浏览 复制 上一个帖子 下一个帖子
标签:

nutz rest post 是不是必须使用 http://127.0.0.1/test/info/select/{"version", "NutzTest","website", Nutz.version()} 这种形式传参。

我用下面方式后台参数是index.html

Map<String, Object> parms = new HashMap<String, Object>();
    parms.put("version", "NutzTest");
    parms.put("website", Nutz.version());
String response = Http.post("http://127.0.0.1/test/info/select/", paramlist, 5 * 1000);

http://127.0.0.1/test/info/select/{"version", "NutzTest","website", Nutz.version()}

5 回复

http包就是对HttpConnection的封装,没有"必须"怎么怎么传参一说

把json直接拼入url是违反http规范的

@At("/jgxx/info")
public class test {
	@AdaptBy(type = JsonAdaptor.class)
	@At("/select/**")
	@POST
	public String getUser(@Param("..") NutMap jgxx) {
		// TODO 这里是实现代码
		System.out.println("jgxx"+jgxx);
		JSONObject json = new JSONObject();
		json.put("code", 200);
		json.put("message", "请求成功!");
		json.put("data", "");
		return json.toString();
	}
}

请求代码

Map<String, Object> parms = new HashMap<String, Object>();
    parms.put("version", "NutzTest");
    parms.put("website", Nutz.version());
String response = Http.post("http://127.0.0.1/test/info/select/", paramlist, 5 * 1000);

报错信息

2019-04-03 17:15:23,667 [WARN][org.nutz.mvc.impl.processor.FailProcessor] - Error@/jgxx/info/select/index :
org.nutz.json.JsonException: !Json syntax error nearby [row:1,col:1 char 'b'], reason: 'Unexpect String = bz=&czhm=&djip=192.168.1.100&djr=61031069'
	at org.nutz.json.impl.JsonTokenScan.readObject(JsonCompileImplV2.java:304)
	at org.nutz.json.impl.JsonTokenScan.read(JsonCompileImplV2.java:360)
	at org.nutz.json.impl.JsonCompileImplV2.parse(JsonCompileImplV2.java:27)
	at org.nutz.json.Json.fromJson(Json.java:42)
	at org.nutz.mvc.adaptor.JsonAdaptor.getReferObject(JsonAdaptor.java:41)
	at org.nutz.mvc.adaptor.AbstractAdaptor.adapt(AbstractAdaptor.java:250)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:25)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:218)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)

这样做可以获取到参数

String response = Http.post("http://127.0.0.1/test/info/select/{"version", "NutzTest","website", Nutz.version()} ", "", 5 * 1000);

你写的代码就不合法,

  1. Http.post的params参数是一个Map, 传字符串会编译错误的
  2. Http.post发的参数是表单, 后端用JsonAdaptor接收当然会报错
  3. @At("/select/**") 会优先匹配路径参数, 所以方法的第一个参数就用路径参数来填充
  4. 你用Http.post在路径里面填json字符串, 只是歪打正着地用一个错误的方式调用一个错误的方法,碰巧得到一个正确的输出

这样写才是你要的东西:

Http.post3("http://127.0.0.1/test/info/select", Json.toJson(params), Header.create().asJsonContentType(), 5 * 1000);

@At("/select")

添加回复
请先登陆
回到顶部