我正在尝试将 JSONArray 从 Android 传递到我的 Spring java 服务器。
我已经尝试了两种方法。一种是将 JSONArray 作为字符串传递并使用 @PathVariable 注释进行捕获。
这样我可以得到 [{"id":6,"numDishes":1,"observations":"false"},{"id":2,"numDishes":3,"observations":"false"}]我想我可以让它这样工作。
我用于此的代码是:
在安卓中
HttpGet request = new HttpGet(serverURL + action);//action already has two params
HttpResponse response = httpclient.execute(target, request);
在服务器到达 /orderService/addOrder/1/[{"id":6,"numDishes":1,"observations":"false"},{"id":2,"numDishes":3,"observations": “错误的”}]
@RequestMapping(method=RequestMethod.GET, value="/addOrder/{tableNumber}/{jsonParam}")
public void addOrder(@PathVariable Integer tableNumber, @PathVariable String jsonParam) {
log.info("String encoded: " + jsonParam);
}
无论如何,我宁愿直接做。就像是
安卓
HttpPost request = new HttpPost(serverURL + action + URLEncoder.encode(paramsString, "UTF-8"));
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
HttpResponse response = httpclient.execute(target, request);
服务器
public class OrderPojo extends ArrayList<DishPojo>{}
@RequestMapping(method=RequestMethod.POST, value="/addOrderPost/{tableNumber}/{jsonParam}")
public void addOrderPost(@PathVariable Integer tableNumber, @RequestBody OrderPojo jsonParam) {
log.info("addOrderPost OrderPojo: " + jsonParam);
}
另外,因为我认为这可能是问题的一部分,所以我在我的 spring-servlet 中有这个:
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter"/>
</list>
</property>
</bean>
<mvc:annotation-driven/>
我在正确的道路上吗?如何将我的对象直接解析为服务器内的自定义对象?