WebService 笔记

前端之家收集整理的这篇文章主要介绍了WebService 笔记前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

WebService服务端框架:jersey

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


Server:

  1. @Configuration
  2. public class ApplicationConfig {
  3. @Named
  4. static class JerseyConfig extends ResourceConfig {
  5. public JerseyConfig() {
  6. this.packages("com.lenovo.li.content.controllers");
  7. }
  8. }
  9.  
  10. @Bean
  11. public ObjectMapper objectMapper() {
  12. ObjectMapper objectMapper = new ObjectMapper();
  13. return objectMapper;
  14. }
  15. }

Pom

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-jersey</artifactId>
  4. </dependency>

Controller

  1. @Named
  2. @Path("/Test")
  3. public class RestController {
  4.  
  5. @POST
  6. @Path("/get/{url}")
  7. @Produces(MediaType.APPLICATION_JSON)
  8. public Test getTestContent(@PathParam("url") final String url, @RequestBody Context context)
  9. throws JsonParseException, JsonMappingException, IOException {
  10. }
  11. @PUT
  12. @Path("/update/{url}")
  13. @Produces(MediaType.APPLICATION_JSON)
  14. public Test updateTestContent(@PathParam("url") final String url, @RequestBody Page page)
  15. throws JsonParseException, IOException {
  16. }
  17. }

Client

  1. public class TestClient{
  2.  
  3. @Autowired
  4. RestTemplate restTemplate;
  5. public String getTestContent(String name, Context context) {
  6. ResponseEntity<String> response = restTemplate.postForEntity(url, context,String.class);
  7. return response.getBody();
  8. }
  9.  
  10. }

ClientXml

  1. <bean id="drHttpRequestFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory"/>
  2. <bean id="restTemplate" class="org.springframework.web.client.RestTemplate" p:requestFactory-ref="drHttpRequestFactory" >
  3.         <property name="messageConverters">
  4.             <list>
  5.                 <bean class="org.springframework.http.converter.StringHttpMessageConverter">
  6.                     <property name="supportedMediaTypes">
  7.                         <list>
  8.                             <value>text/plain;charset=UTF-8</value>
  9.                         <value>text/html;charset=UTF-8</value>  
  10.                         <value>application/json; charset=UTF-8</value>  
  11.                         </list>
  12.                     </property>
  13.                 </bean>
  14.                 <ref bean="mappingJackson2HttpMessageConverter"/>
  15.                 
  16.                 <bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
  17.             </list>
  18.         </property>
  19. </bean>

猜你在找的WebService相关文章