Way to Consume Json request as Json object in Jersey Rest Service

戏子无情 提交于 2019-12-19 11:47:39

问题


Hi I tried googling around but cannot find solution that I want to achieve.

Example to map json to java object we do

 @POST
 @Consumes(application/json)
 @Produces(application/json)
 public Response createUpdateDeleteManualClinicalData(MyJavaPojo definedPojo) {
   // this maps any json to a java object, but in my case I am dealing with generic json structure
}

What I want to achieve is Keep it as json object itself

public Response createUpdateDeleteManualClinicalData(JSONObject json)

Work around: I can get data as plain text and convert that to json. But its an overhead from my side which I want to avoid.

Edit: I am open to using any Json library like JsonNode etc... as far as I get Json object Structure directly without the overhead of String to Json from my side. This should be common usage or am I missing some core concept here.


回答1:


It was a straight solution that I happened to overlooked... my bad. Jersey is way smarter than I thought... curious to know what magic happens under the layers

@POST
 @Consumes(application/json)
 @Produces(application/json)
 public Response createUpdateDeleteManualClinicalData(JsonNode jsonNode) {
//jsoNode body casted automatically into JsonNode
}



回答2:


What is the web.xml configuration you are using? Is it something similar to here web.xml setup gists for Jersey1 and Jackson2?




回答3:


If you are using the same configuration as web.xml setup gists for Jersey1 and Jackson2, then you may do as below. This is a possible alternative than using JSONObject

@POST
@Path("/sub_path2")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Map<String,Object> saveMethod( Map<String,Object> params  ) throws IOException {

    // Processing steps

    return params;
}


来源:https://stackoverflow.com/questions/19451644/way-to-consume-json-request-as-json-object-in-jersey-rest-service

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!