WebService服务端框架:jersey

WebService调用方式:jersey、http、spring RestTemplate


Server:

@ConfigurationpublicclassApplicationConfig{@NamedstaticclassJerseyConfigextendsResourceConfig{publicJerseyConfig(){this.packages("com.lenovo.li.content.controllers");}}@BeanpublicObjectMapperobjectMapper(){ObjectMapperobjectMapper=newObjectMapper();returnobjectMapper;}}

Pom

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jersey</artifactId></dependency>

Controller

@Named@Path("/Test")publicclassRestController{@POST@Path("/get/{url}")@Produces(MediaType.APPLICATION_JSON)publicTestgetTestContent(@PathParam("url")finalStringurl,@RequestBodyContextcontext)throwsJsonParseException,JsonMappingException,IOException{}@PUT@Path("/update/{url}")@Produces(MediaType.APPLICATION_JSON)publicTestupdateTestContent(@PathParam("url")finalStringurl,@RequestBodyPagepage)throwsJsonParseException,JsonMappingException,IOException{}}

Client

publicclassTestClient{@AutowiredRestTemplaterestTemplate;publicStringgetTestContent(Stringname,Contextcontext){ResponseEntity<String>response=restTemplate.postForEntity(url,context,String.class);returnresponse.getBody();}}

ClientXml

<beanid="drHttpRequestFactory"class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory"/><beanid="restTemplate"class="org.springframework.web.client.RestTemplate"p:requestFactory-ref="drHttpRequestFactory"><propertyname="messageConverters"><list><beanclass="org.springframework.http.converter.StringHttpMessageConverter"><propertyname="supportedMediaTypes"><list><value>text/plain;charset=UTF-8</value><value>text/html;charset=UTF-8</value><value>application/json;charset=UTF-8</value></list></property></bean><refbean="mappingJackson2HttpMessageConverter"/><beanclass="org.springframework.http.converter.FormHttpMessageConverter"/></list></property></bean>