Rest begin数组,但字符串为GSON
我有一个Web服务REST:Rest begin数组,但字符串为GSON,rest,exception,gson,Rest,Exception,Gson,我有一个Web服务REST: @Path("/Vehicles") public class Vehicles{ @GET @Path("/Cars") @Produces(aplicattion/json) public String Cars() { Car[] cars = Consulting my database... Gson gson = new Gson(
@Path("/Vehicles")
public class Vehicles{
@GET
@Path("/Cars")
@Produces(aplicattion/json)
public String Cars() {
Car[] cars = Consulting my database...
Gson gson = new Gson();
return gson.toJson(cars);
}
我使用web服务:
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(
"http://localhost:8080/Concessionaire/rest/Vehicles/Cars");
HttpResponse resp = httpClient.execute(get);
String respGET = EntityUtils.toString(resp.getEntity());
Gson gson = new Gson();
Cars[] c = gson.fromJson(respGET,Cars[].class);
}catch(Exception e){
}
但出现此异常:应为BEGIN_数组,但在第1行第6列处为字符串
有什么问题吗?您的方法返回一个字符串
public String Cars()
客户端代码需要一个Car数组
Cars[] c = gson.fromJson(respGET,Cars[].class);
Gson在解析json时需要BEGIN_数组事件,但会找到一个字符串。要修复它,请使用jerseyResponse
类发送Cars[],并将返回类型更改为Response
return Response.ok(myCarsArray).build();