Several common methods for passing parameters between Json and Java

Source: Internet
Author: User
Tags findone tojson

1) PASS Parameters in Get Mode

Parameter passing through Get is a typical method of parameter passing through a URL. Simply add the variable and the value to be passed after the URL. This method is relatively simple. The following example shows how to use it.


URL:

Http: // localhost: 8080/favccxx/rs/findInspectJobByJobId? JobId = 525b8818d52cc6f835bb9e41

Java source code:

@GET@Path("/findInspectJobByJobId")@Produces({ MediaType.TEXT_XML })public String findInspectJobByJobId(@QueryParam("jobId") String jobId,@Context HttpServletRequest request) {       InspectJob inspectJob = inspectJobService.findOne("jobId", jobId);return JsonUtil.toJson(inspectJob);    }
(2) Pass Parameter 2.1 in POST mode and use URL as a variable.

Treat the last location of the URL as a variable, as shown in the following 000, which is both the URL address and the parameter to be passed.

URL:

Http: // localhost: 8080/favccxx/rs/getInspectJobByJobId/000

Java source code:


@POST@Path("/getInspectJobByJobId/{jobId}")@Produces({ MediaType.TEXT_XML })public String getInspectJobByJobId(@PathParam("jobId") String jobId,@Context HttpServletRequest request) {       InspectJob inspectJob = inspectJobService.findOne("jobId", jobId);return JsonUtil.toJson(inspectJob);    }
2.2 use a traditional & connector to pass multiple parameters

I always mistakenly believe that Json is used to pass parameters to the java end in Json format. Actually, it is not. By default, json still uses the = and & connectors to pass parameters to the backend. The sample code is as follows:


URL:

Http: // localhost: 8080/favccxx/rs/queryInspectJobById

Parameters:

JobId = 123456 & jobName = A task

Java source code:

@POST@Path("/queryInspectJobById")@Produces({ MediaType.APPLICATION_JSON })public String queryInspectJobById(@FormParam("jobId") String jobId,@FormParam("jobName") String jobName, @Context HttpServletRequest request) {       System.out.println("jobId:"+ jobId + ",AndjobName is:" + jobName) ;       InspectJob inspectJob = inspectJobService.findOne("jobId", jobId);return JsonUtil.toJson(inspectJob);    }
2.3 transmitted in Json format and parsed parameters using the POJO class

When you use this method to pass parameters, the variables in Json format must be parameters in the Pojo class. This allows you to flexibly pass parameters, and the backend parses the parameters based on actual needs for business processing.


URL:

Http: // localhost: 8080/favccxx/rs/queryInspectJobByJobId

Parameters:

{

"JobId": "123456 ",

"JobName": "task"

}

Java source code:

@POST@Path("/queryInspectJobByJobId")@Produces({ MediaType.APPLICATION_JSON })public String queryInspectJobByJobId(@RequestBody String body,@Context HttpServletRequest request) {       InspectJob inspectJobReq = JsonUtil.fromJson(body, InspectJob.class);       InspectJob inspectJob = inspectJobService.findOne("jobId", inspectJobReq.getJobId());return JsonUtil.toJson(inspectJob);    }
2.4 parameters are transmitted in Json format and QueryMap is a custom class for querying by page.

The format of parameters transmitted at the front end remains unchanged. The backend encapsulates a QueryMap class to query paging-related data.

URL:

Http: // localhost: 8080/favccxx/rs/pageQueryJob

Parameters:

{

"PageIndex": 2,

"QueryMap ":{

"JobId": "525b8818d52cc6f835bb9e41"

}

}

Java source code:

@POST@Path("/pageQueryJob")@Produces({ MediaType.APPLICATION_JSON })public String pageQueryJob(@RequestBody String body,@Context HttpServletRequest request){       PageQuery pageQuery= JsonUtil.fromJson(body,PageQuery.class);       String jobId = pageQuery.getQueryMapByKey("jobId").toString();returnnull;    }


This article from the "dust wind with the shadows of the Sky" blog, please be sure to keep this source http://genuinecx.blog.51cto.com/2890523/1325175

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.