WebService 笔记
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>
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。