spring-boot-api-project-seed icon indicating copy to clipboard operation
spring-boot-api-project-seed copied to clipboard

我改了restful模版之后, 测试用post添加add一个用户的,失败了

Open ithjz opened this issue 6 years ago • 6 comments

`String url = "http://localhost:8080/user";//473 255

	Map<String, String> params = new HashMap<String, String>();
	String encode = "UTF-8";
	params.put("username", "rcyj_20180");
	params.put("password", "78sexwqeqw");
	params.put("sex", "1");
	params.put("nickName", "战国策");

	params.put("openId", "51jxty&rsv_dl");
	params.put("receiverCity", "深圳");
	params.put("receiverDistrict", "南山区");

	params.put("receiverAddress", "深圳市科技园");

	params.put("receiverZip", "580320");

	// 填入各个表单域的值

	getHttpPost(url, params, encode);`

[nio-8080-exec-5] c.c.project.configurer.WebMvcConfigurer : 接口 [/user] 出现异常,方法:com.company.project.web.UserController.add,异常摘要:JSON parse error: syntax error, expect {, actual error, pos 0, fastjson-version 1.2.44; nested exception is com.alibaba.fastjson.JSONException: syntax error, expect {, actual error, pos 0, fastjson-version 1.2.44

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: syntax error, expect {, actual error, pos 0, fastjson-version 1.2.44; nested exception is com.alibaba.fastjson.JSONException: syntax error, expect {, actual error, pos 0, fastjson-version 1.2.44 at com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter.readType(FastJsonHttpMessageConverter.java:206) ~[fastjson-1.2.44.jar:na] at com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter.read(FastJsonHttpMessageConverter.java:178) ~[fastjson-1.2.44.jar:na] at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:201) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:150) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:128) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.me

ithjz avatar Aug 16 '18 08:08 ithjz

json转换问题,debug一下就知道了吧,跟框架没关系

a173030685 avatar Aug 20 '18 09:08 a173030685

@a173030685 你好, 我之前试过, post接口模版的,几乎是一样的参数,为啥到了restful就不行了呢?

ithjz avatar Aug 20 '18 09:08 ithjz

@a173030685 方便发一下你的基于restful的 添加用户的例子代码给我看看吗?

ithjz avatar Aug 20 '18 09:08 ithjz

@lihengming @a173030685 有restful的 新增用户的代码例子吗

ithjz avatar Aug 20 '18 10:08 ithjz

修改controller.ftl模板文件就行了,把@PostMapping改成你想要的

a173030685 avatar Aug 21 '18 02:08 a173030685

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/json"); RequestBody body = RequestBody.create(mediaType, "{\n "name": "Hello-World6",\n "description": "This is your first repository",\n "homepage": "https://github.com",\n "private": false,\n "has_issues": true,\n "has_wiki": 12,\n "has_downloads": "121"\n}");

ithjz avatar Aug 23 '18 09:08 ithjz