简体   繁体   中英

Custom JSON entity parsing in Google App Engine endpoint

I have to transform a very peculiar JSON payload into POJOs manually. I thought I could put the JSON string into a String entity:

@ApiMethod(
  name = "postSomething",
  path = "postSomething/{id}",
  httpMethod = ApiMethod.HttpMethod.POST
)
public void postSomething(@Named("id") Integer id, HttpServletRequest request, String data) {
  //Parse data here...
}

When I do that, I get an error: MissingParameterNameException: Missing parameter name. Parameter type (class java.lang.String) is not an entity type and thus should be annotated with @Named.

I tried to use an @ApiTransformer but I get a similar error.

Could you please give me an example of parsing the JSON content manually?

错误消息指出String数据需要具有@Named批注,类似于Integer id。

I worked around this issue by using a Collection s class instead of String and manual parsing:

@ApiMethod(
  name = "postSomething",
  path = "postSomething/{id}",
  httpMethod = ApiMethod.HttpMethod.POST
)

public void postSomething(@Named("id") Integer id, HttpServletRequest request, HashMap<String,String> data) {
  //Parse each item of data here...
}

From this, I can parse each item inside the data . The values contain a hierarchy of either other collections ( List for an array, Map for a JSON entity) or String for an actual value. So by doing this I don't need to use any other JSON parsing library such as Jackson.

String is not an @Entity object so it can't be passed as a parameter (a data parameter) to the endpoints API without proper annotation (like @Name or @Nullable). Either you must remove it from the method declaration or annotate it with @Name or @Nullable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM