介绍
这篇文章将为大家详细讲解有关Restful服务如何利用球衣来实现,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
<强> 1。创建maven-web工程,后面就是正常的maven工程创建流程。强>
<强> 2。添加泽框架的maven文件。强>
& lt; project xmlns=癶ttp://maven.apache.org/POM/4.0.0", xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance" ,xsi: schemaLocation=癶ttp://maven.apache.org/POM/4.0.0 , http://maven.apache.org/maven-v4_0_0.xsd"比; ,& lt; modelVersion> 4.0.0 ,& lt; groupId> com.restful ,& lt; artifactId> jerseyDemo ,& lt; packaging> war ,& lt; version> 1.0 -snapshot ,& lt; name> jerseyDemo Maven  Webapp ,& lt; url> http://maven.apache.org</url> ,& lt; dependencies> ,& lt; dependency> & lt;才能groupId> junit</groupId> & lt;才能artifactId> junit</artifactId> & lt;才能version> 3.8.1</version> & lt;才能scope> test</scope> ,& lt;/dependency> ,& lt; dependency> & lt;才能groupId> org.glassfish.jersey.containers</groupId> & lt;才能artifactId> jersey-container-servlet</artifactId> & lt;才能version> 2.9 & lt;/version> ,& lt;/dependency> ,& lt; dependency> & lt;才能groupId> org.glassfish.jersey.core</groupId> & lt;才能artifactId> jersey-client</artifactId> & lt;才能version> 2.9 & lt;/version> ,& lt;/dependency> ,& lt; dependency> & lt;才能groupId> org.glassfish.jersey.media</groupId> & lt;才能artifactId> jersey-media-json-jackson</artifactId> & lt;才能version> 2.9 & lt;/version> ,& lt;/dependency> ,& lt; dependency> & lt;才能groupId> com.sun.jersey</groupId> & lt;才能artifactId> jersey-client</artifactId> & lt;才能version> 1.19.3</version> ,& lt;/dependency> ,& lt;/dependencies> ,& lt; build> ,& lt; finalName> jerseyDemo ,& lt;/build> & lt;/project>
<强> 3。Restful接口定义。强>
package com.restful.service; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.restful.entity.PersonEntity; import javax.ws.rs。*; import javax.ws.rs.core.MediaType; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import 并不知道; import java.util.Map;/* * ,* Created by  XuHui 提醒2017/8/2。 ,*/@ path (“/JerseyService") public class  JerseyService { ,private static  Map<字符串,PersonEntity>, map =, new HashMap<字符串,PersonEntity> (); ,@ get @ path (“/getAllResource"), ,与@ (MediaType.APPLICATION_JSON) ,public String  getAllResource (), throws JsonProcessingException { List<才能;PersonEntity>, list =, new ArrayList(); for 才能;(PersonEntity entity : map.values ()), { ,,list.add(实体); ,,} ObjectMapper 才能;mapper =, new objectmap (); return 才能mapper.writeValueAsString(列表); ,} ,@ get ,@ path (“/getResourceById/{id}“) ,与@ (MediaType.APPLICATION_JSON) ,public String  getResource (@PathParam (“id"), String id), throws JsonProcessingException { ObjectMapper 才能;mapper =, new objectmap (); return 才能;mapper.writeValueAsString (map.get (id)); ,} ,@POST ,@ path (“/addResource/{人}“) (,了@ consumes注解{MediaType.APPLICATION_XML,, MediaType.APPLICATION_JSON}) ,与@ (MediaType.APPLICATION_JSON) ,public String  addResource (String 人),throws IOException { ObjectMapper 才能;mapper =, new objectmap (); PersonEntity 才能;entity =, mapper.readValue(人,,PersonEntity.class); map.put才能(entity.getId(),实体); return 才能mapper.writeValueAsString(实体); ,} ,@ get @ path (“/getRandomResource"), ,与@ (MediaType.APPLICATION_JSON) ,public String  getRandomResource (), throws JsonProcessingException { ObjectMapper 才能;mapper =, new objectmap (); PersonEntity 才能;entity =, new PersonEntity (“NO1",,“Joker",,“http:///"); null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null 宁静的服务如何利用球衣来实现