jersey put 服务_项目学生:带有Jersey的Web服务服务器
jersey put 服務(wù)
這是Project Student的一部分。 其他職位包括帶有Jersey的Webservice Client , 業(yè)務(wù)層和帶有Spring Data的持久性 。
RESTful Web應(yīng)用程序洋蔥的第二層是Web服務(wù)服務(wù)器。 它應(yīng)該是一個(gè)薄層,用于包裝對(duì)業(yè)務(wù)層的調(diào)用,但不對(duì)其自身進(jìn)行大量處理。 這篇文章有很多代碼,但主要是測(cè)試類。
設(shè)計(jì)決策
澤西島 -我將澤西島用于REST服務(wù)器。 我考慮了替代方案-Spring MVC , Netty等,但出于與客戶相同的原因,決定選擇Jersey。 它輕巧,不會(huì)限制開發(fā)人員。
依賴注入 –我需要依賴注入,這意味著我需要確定一個(gè)框架:Spring,EJB3,Guice等。我已經(jīng)知道我將在持久層中使用Spring Data ,因此使用它很容易春天的框架。 我仍然會(huì)謹(jǐn)慎地最小化該框架上的任何依賴關(guān)系(ha!),以實(shí)現(xiàn)最大的靈活性。
局限性
球衣 –我不知道球衣將如何處理高負(fù)荷。 這是REST服務(wù)器必須是業(yè)務(wù)層的薄包裝的關(guān)鍵原因-如果有必要,更改庫將相對(duì)容易。
用戶權(quán)限 –沒有嘗試將對(duì)某些方法的訪問限制為特定用戶或主機(jī)。 這應(yīng)該由業(yè)務(wù)層來處理,而安全性異常將由REST服務(wù)器轉(zhuǎn)換為FORBIDDEN狀態(tài)代碼。
澤西REST服務(wù)器
REST API是我們?cè)缙诘脑O(shè)計(jì)文檔之一。 對(duì)于服務(wù)器,這意味著我們從REST服務(wù)器開始而不是從業(yè)務(wù)層API開始實(shí)現(xiàn)該層。 實(shí)際上,REST服務(wù)器在業(yè)務(wù)層API中定義了必要的方法。
與標(biāo)準(zhǔn)的REST CRUD API有一個(gè)小的偏差:對(duì)象是使用POST而不是PUT創(chuàng)建的,因?yàn)楹笳叩恼Z義是完全按照提供的方式創(chuàng)建了對(duì)象。 我們無法做到這一點(diǎn)–出于安全原因,我們從不公開內(nèi)部ID,也不得接受用戶定義的UUID。 這意味著我們將違反REST API合同,因此我們改用POST。
還有一個(gè)小作弊:CRUD合同僅需要具有創(chuàng)建或更新對(duì)象的能力。 這意味著我們只需給出路徑就可以找出所需的操作–我們不需要添加特定的“操作”字段。 隨著我們將實(shí)現(xiàn)擴(kuò)展到不僅僅包括CRUD動(dòng)作,這可能會(huì)改變。
繼續(xù)執(zhí)行代碼...
@Service @Path("/course") public class CourseResource extends AbstractResource {private static final Logger log = Logger.getLogger(CourseResource.class);private static final Course[] EMPTY_COURSE_ARRAY = new Course[0];@ContextUriInfo uriInfo;@ContextRequest request;@Resourceprivate CourseService service;/*** Default constructor.*/public CourseResource() {}/*** Unit test constructor.* * @param service*/CourseResource(CourseService service) {this.service = service;}/*** Get all Courses.* * @return*/@GET@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })public Response findAllCourses() {log.debug("CourseResource: findAllCourses()");Response response = null;try {List<Course> courses = service.findAllCourses();List<Course> results = new ArrayList<Course>(courses.size());for (Course course : courses) {results.add(scrubCourse(course));}response = Response.ok(results.toArray(EMPTY_COURSE_ARRAY)).build();} catch (Exception e) {if (!(e instanceof UnitTestException)) {log.info("unhandled exception", e);}response = Response.status(Status.INTERNAL_SERVER_ERROR).build();}return response;}/*** Create a Course.* * @param req* @return*/@POST@Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })public Response createCourse(Name req) {log.debug("CourseResource: createCourse()");final String name = req.getName();if ((name == null) || name.isEmpty()) {return Response.status(Status.BAD_REQUEST).entity("'name' is required").build();}Response response = null;try {Course course = service.createCourse(name);if (course == null) {response = Response.status(Status.INTERNAL_SERVER_ERROR).build();} else {response = Response.created(URI.create(course.getUuid())).entity(scrubCourse(course)).build();}} catch (Exception e) {if (!(e instanceof UnitTestException)) {log.info("unhandled exception", e);}response = Response.status(Status.INTERNAL_SERVER_ERROR).build();}return response;}/*** Get a specific Course.* * @param uuid* @return*/@Path("/{courseId}")@GET@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })public Response getCourse(@PathParam("courseId") String id) {log.debug("CourseResource: getCourse()");Response response = null;try {Course course = service.findCourseByUuid(id);response = Response.ok(scrubCourse(course)).build();} catch (ObjectNotFoundException e) {response = Response.status(Status.NOT_FOUND).build();} catch (Exception e) {if (!e instanceof UnitTestException)) {log.info("unhandled exception", e);}response = Response.status(Status.INTERNAL_SERVER_ERROR).build();}return response;}/*** Update a Course.* * FIXME: what about uniqueness violations?* * @param id* @param req* @return*/@Path("/{courseId}")@POST@Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })public Response updateCourse(@PathParam("courseId") String id, Name req) {log.debug("CourseResource: updateCourse()");final String name = req.getName();if ((name == null) || name.isEmpty()) {return Response.status(Status.BAD_REQUEST).entity("'name' is required").build();}Response response = null;try {final Course course = service.findCourseByUuid(id);final Course updatedCourse = service.updateCourse(course, name);response = Response.ok(scrubCourse(updatedCourse)).build();} catch (ObjectNotFoundException exception) {response = Response.status(Status.NOT_FOUND).build();} catch (Exception e) {if (!(e instanceof UnitTestException)) {log.info("unhandled exception", e);}response = Response.status(Status.INTERNAL_SERVER_ERROR).build();}return response;}/*** Delete a Course.* * @param id* @return*/@Path("/{courseId}")@DELETEpublic Response deleteCourse(@PathParam("courseId") String id) {log.debug("CourseResource: deleteCourse()");Response response = null;try {service.deleteCourse(id);response = Response.noContent().build();} catch (ObjectNotFoundException exception) {response = Response.noContent().build();} catch (Exception e) {if (!(e instanceof UnitTestException)) {log.info("unhandled exception", e);}response = Response.status(Status.INTERNAL_SERVER_ERROR).build();}return response;} }該實(shí)現(xiàn)告訴我們,我們需要三件事:
- 服務(wù)API(CourseService)
- 請(qǐng)求參數(shù)類(名稱)
- 洗滌器(scrubCourse)
我沒有顯示完整的日志記錄。 必須清除請(qǐng)求參數(shù),以避免日志污染。 。 作為一個(gè)簡(jiǎn)單的示例,請(qǐng)考慮使用寫入SQL數(shù)據(jù)庫的記錄器,以簡(jiǎn)化分析。 這個(gè)記錄器的一個(gè)簡(jiǎn)單的實(shí)現(xiàn)-不使用位置參數(shù)-將允許通過精心設(shè)計(jì)的請(qǐng)求參數(shù)進(jìn)行SQL注入!
OWASP ESAPI包含可用于日志清理的方法。 我沒有包含在這里,因?yàn)樵O(shè)置起來有些麻煩。 (應(yīng)該很快就會(huì)在簽入代碼中。)
為什么要登錄到數(shù)據(jù)庫? 一種好的做法是記錄到達(dá)服務(wù)器層的所有未處理的異常-您永遠(yuǎn)都不想依靠用戶來報(bào)告問題,而且寫入日志文件的錯(cuò)誤很容易被忽略。 相反,使用簡(jiǎn)單工具可以輕松檢查寫入數(shù)據(jù)庫的報(bào)告。
發(fā)生未處理的異常時(shí),高級(jí)開發(fā)人員甚至可以創(chuàng)建新的錯(cuò)誤報(bào)告。 在這種情況下,至關(guān)重要的是維護(hù)一個(gè)單獨(dú)的異常數(shù)據(jù)庫,以避免提交重復(fù)的條目和使開發(fā)人員不知所措。 (數(shù)據(jù)庫可以包含每個(gè)異常的詳細(xì)信息,但錯(cuò)誤報(bào)告系統(tǒng)每個(gè)異常類+堆棧跟蹤僅應(yīng)有一個(gè)錯(cuò)誤報(bào)告。)
服務(wù)API
CRUD操作的服務(wù)API很簡(jiǎn)單。
public interface CourseService {List<Course> findAllCourses();Course findCourseById(Integer id);Course findCourseByUuid(String uuid);Course createCourse(String name);Course updateCourse(Course course, String name);void deleteCourse(String uuid); }該API還包含一個(gè)ObjectNotFoundException。 (這應(yīng)該擴(kuò)展為包括找不到的對(duì)象的類型。)
public class ObjectNotFoundException extends RuntimeException {private static final long serialVersionUID = 1L;private final String uuid;public ObjectNotFoundException(String uuid) {super("object not found: [" + uuid + "]");this.uuid = uuid;}public String getUuid() {return uuid;} }如上所述,我們最終還需要一個(gè)UnauthorizedOperationException。
請(qǐng)求參數(shù)
請(qǐng)求參數(shù)是封裝了POST負(fù)載的簡(jiǎn)單POJO。
@XmlRootElement public class Name {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;} }學(xué)生和教師也需要電子郵件地址。
@XmlRootElement public class NameAndEmailAddress {private String name;private String emailAddress;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getEmailAddress() {return emailAddress;}public void setEmailAddress(String emailAddress) {this.emailAddress = emailAddress;} }最終的應(yīng)用程序?qū)⒕哂写罅康恼?qǐng)求參數(shù)類。
洗滌塔
洗滌塔具有三個(gè)目的。 首先,它刪除了不應(yīng)提供給客戶端的敏感信息,例如內(nèi)部數(shù)據(jù)庫標(biāo)識(shí)符。
其次,它可以防止由于引入集合而導(dǎo)致大量的數(shù)據(jù)庫轉(zhuǎn)儲(chǔ)。 例如,一個(gè)學(xué)生應(yīng)包括當(dāng)前部分的列表,但每個(gè)部分都有已注冊(cè)的學(xué)生和教師的列表。 這些學(xué)生和教師中的每一個(gè)都有自己的當(dāng)前部分列表。 進(jìn)行泡沫,沖洗,重復(fù),最終將整個(gè)數(shù)據(jù)庫轉(zhuǎn)儲(chǔ)以響應(yīng)單個(gè)查詢。
解決方案是僅包含有關(guān)每個(gè)可以獨(dú)立查詢的對(duì)象的淺層信息。 例如,一個(gè)學(xué)生將擁有當(dāng)前部分的列表,但是這些部分將僅包含UUID和名稱。 一條很好的經(jīng)驗(yàn)法則是,清理后的集合應(yīng)完全包含將在下拉列表和表示表中使用的信息,僅此而已。 演示列表可以包含鏈接(或AJAX操作),以根據(jù)需要提取其他信息。
最后,這是執(zhí)行HTML編碼和清理的好地方。 應(yīng)該清除返回的值,以防止跨站點(diǎn)腳本(CSS)攻擊 。
public abstract class AbstractResource {/*** Scrub 'course' object.** FIXME add HTML scrubbing and encoding for string values!*/ public Course scrubCourse(final Course dirty) {final Course clean = new Course();clean.setUuid(dirty.getUuid());clean.setName(dirty.getName());// clean.setSelf("resource/" + dirty.getUuid());return clean;} }配置類
我們有兩個(gè)配置類。 第一個(gè)始終由服務(wù)器使用,第二個(gè)僅在集成測(cè)試期間由服務(wù)器使用。 后者的配置(和引用的類)位于集成測(cè)試源樹中。
我更喜歡使用配置類(在Spring 3.0中引入),因?yàn)樗鼈兲峁┝俗畲蟮撵`活性-例如,我可以根據(jù)運(yùn)行應(yīng)用程序或環(huán)境變量的用戶有條件地定義bean-并允許我仍然包括標(biāo)準(zhǔn)配置文件。
@Configuration @ComponentScan(basePackages = { "com.invariantproperties.sandbox.student.webservice.server.rest" }) @ImportResource({ "classpath:applicationContext-rest.xml" }) // @PropertySource("classpath:application.properties") public class RestApplicationContext {@Resourceprivate Environment environment; }Spring 3.1引入了配置文件。 它們可以工作-但是我正在使用的具有彈簧意識(shí)的jersey servlet似乎無法正確設(shè)置活動(dòng)概要文件。
@Configuration //@Profile("test") public class RestApplicationContextTest {@BeanStudentService studentService() {return new DummyStudentService();} }web.xml
現(xiàn)在,我們有足夠的資源來實(shí)現(xiàn)我們的Web服務(wù)器。 使用的servlet是啟用了spring的Jersey servlet,它使用contextClass參數(shù)中給出的配置類。 (也可以使用配置文件,但不能使用配置類和文件的組合。)
該Servlet還包含spring.profiles.active的定義。 目的是通過spring 3.1 @Profile注釋有條件地在RestApplicationContextTest中包含定義,但我無法使其正常工作。 我把它留給以后參考。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><display-name>Project Student Webservice</display-name><context-param><param-name>contextClass</param-name><param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value></context-param><context-param><param-name>contextConfigLocation</param-name><param-value>com.invariantproperties.sandbox.student.webservice.server.config.RestApplicationContextcom.invariantproperties.sandbox.student.webservice.server.config.RestApplicationContextTest</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>REST dispatcher</servlet-name><servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class><init-param><param-name>spring.profiles.active</param-name><param-value>test</param-value></init-param></servlet><servlet-mapping><servlet-name>REST dispatcher</servlet-name><url-pattern>/rest/*</url-pattern></servlet-mapping> </web-app>單元測(cè)試
單元測(cè)試很簡(jiǎn)單。
public class CourseResourceTest {private Course physics = new Course();private Course mechanics = new Course();@Beforepublic void init() {physics.setId(1);physics.setName("physics");physics.setUuid(UUID.randomUUID().toString());mechanics.setId(1);mechanics.setName("mechanics");mechanics.setUuid(UUID.randomUUID().toString());}@Testpublic void testFindAllCourses() {final List<Course> expected = Arrays.asList(physics);final CourseService service = Mockito.mock(CourseService.class);when(service.findAllCourses()).thenReturn(expected);final CourseResource resource = new CourseResource(service);final Response response = resource.findAllCourses();assertEquals(200, response.getStatus());final Course[] actual = (Course[]) response.getEntity();assertEquals(expected.size(), actual.length);assertNull(actual[0].getId());assertEquals(expected.get(0).getName(), actual[0].getName());assertEquals(expected.get(0).getUuid(), actual[0].getUuid());}@Testpublic void testFindAllCoursesEmpty() {final List<Course> expected = new ArrayList<>();final CourseService service = Mockito.mock(CourseService.class);when(service.findAllCourses()).thenReturn(expected);final CourseResource resource = new CourseResource(service);final Response response = resource.findAllCourses();assertEquals(200, response.getStatus());final Course[] actual = (Course[]) response.getEntity();assertEquals(0, actual.length);}@Testpublic void testFindAllCoursesFailure() {final CourseService service = Mockito.mock(CourseService.class);when(service.findAllCourses()).thenThrow(new UnitTestException();final CourseResource resource = new CourseResource(service);final Response response = resource.findAllCourses();assertEquals(500, response.getStatus());}@Testpublic void testGetCourse() {final Course expected = physics;final CourseService service = Mockito.mock(CourseService.class);when(service.findCourseByUuid(expected.getUuid())).thenReturn(expected);final CourseResource resource = new CourseResource(service);final Response response = resource.getCourse(expected.getUuid());assertEquals(200, response.getStatus());final Course actual = (Course) response.getEntity();assertNull(actual.getId());assertEquals(expected.getName(), actual.getName());assertEquals(expected.getUuid(), actual.getUuid());}@Testpublic void testGetCourseMissing() {final CourseService service = Mockito.mock(CourseService.class);when(service.findCourseByUuid(physics.getUuid())).thenThrow(new ObjectNotFoundException(physics.getUuid()));final CourseResource resource = new CourseResource(service);final Response response = resource.getCourse(physics.getUuid());assertEquals(404, response.getStatus());}@Testpublic void testGetCourseFailure() {final CourseService service = Mockito.mock(CourseService.class);when(service.findCourseByUuid(physics.getUuid())).thenThrow(new UnitTestException();final CourseResource resource = new CourseResource(service);final Response response = resource.getCourse(physics.getUuid());assertEquals(500, response.getStatus());}@Testpublic void testCreateCourse() {final Course expected = physics;final Name name = new Name();name.setName(expected.getName());final CourseService service = Mockito.mock(CourseService.class);when(service.createCourse(name.getName())).thenReturn(expected);final CourseResource resource = new CourseResource(service);final Response response = resource.createCourse(name);assertEquals(201, response.getStatus());final Course actual = (Course) response.getEntity();assertNull(actual.getId());assertEquals(expected.getName(), actual.getName());}@Testpublic void testCreateCourseBlankName() {final Course expected = physics;final Name name = new Name();final CourseService service = Mockito.mock(CourseService.class);final CourseResource resource = new CourseResource(service);final Response response = resource.createCourse(name);assertEquals(400, response.getStatus());}/*** Test handling when the course can't be created for some reason. For now* the service layer just returns a null value - it should throw an* appropriate exception.*/@Testpublic void testCreateCourseProblem() {final Course expected = physics;final Name name = new Name();name.setName(expected.getName());final CourseService service = Mockito.mock(CourseService.class);when(service.createCourse(name.getName())).thenReturn(null);final CourseResource resource = new CourseResource(service);final Response response = resource.createCourse(name);assertEquals(500, response.getStatus());}@Testpublic void testCreateCourseFailure() {final Course expected = physics;final Name name = new Name();name.setName(expected.getName());final CourseService service = Mockito.mock(CourseService.class);when(service.createCourse(name.getName())).thenThrow(new UnitTestException();final CourseResource resource = new CourseResource(service);final Response response = resource.createCourse(name);assertEquals(500, response.getStatus());}@Testpublic void testUpdateCourse() {final Course expected = physics;final Name name = new Name();name.setName(mechanics.getName());final Course updated = new Course();updated.setId(expected.getId());updated.setName(mechanics.getName());updated.setUuid(expected.getUuid());final CourseService service = Mockito.mock(CourseService.class);when(service.findCourseByUuid(expected.getUuid())).thenReturn(expected);when(service.updateCourse(expected, name.getName())).thenReturn(updated);final CourseResource resource = new CourseResource(service);final Response response = resource.updateCourse(expected.getUuid(),name);assertEquals(200, response.getStatus());final Course actual = (Course) response.getEntity();assertNull(actual.getId());assertEquals(mechanics.getName(), actual.getName());assertEquals(expected.getUuid(), actual.getUuid());}/*** Test handling when the course can't be updated for some reason. For now* the service layer just returns a null value - it should throw an* appropriate exception.*/@Testpublic void testUpdateCourseProblem() {final Course expected = physics;final Name name = new Name();name.setName(expected.getName());final CourseService service = Mockito.mock(CourseService.class);when(service.updateCourse(expected, name.getName())).thenReturn(null);final CourseResource resource = new CourseResource(service);final Response response = resource.createCourse(name);assertEquals(500, response.getStatus());}@Testpublic void testUpdateCourseFailure() {final Course expected = physics;final Name name = new Name();name.setName(expected.getName());final CourseService service = Mockito.mock(CourseService.class);when(service.updateCourse(expected, name.getName())).thenThrow(new UnitTestException();final CourseResource resource = new CourseResource(service);final Response response = resource.createCourse(name);assertEquals(500, response.getStatus());}@Testpublic void testDeleteCourse() {final Course expected = physics;final CourseService service = Mockito.mock(CourseService.class);doNothing().when(service).deleteCourse(expected.getUuid());final CourseResource resource = new CourseResource(service);final Response response = resource.deleteCourse(expected.getUuid());assertEquals(204, response.getStatus());}@Testpublic void testDeleteCourseMissing() {final Course expected = physics;final Name name = new Name();name.setName(expected.getName());final CourseService service = Mockito.mock(CourseService.class);doThrow(new ObjectNotFoundException(expected.getUuid())).when(service).deleteCourse(expected.getUuid());final CourseResource resource = new CourseResource(service);final Response response = resource.deleteCourse(expected.getUuid());assertEquals(204, response.getStatus());}@Testpublic void testDeleteCourseFailure() {final Course expected = physics;final CourseService service = Mockito.mock(CourseService.class);doThrow(new UnitTestException()).when(service).deleteCourse(expected.getUuid());final CourseResource resource = new CourseResource(service);final Response response = resource.deleteCourse(expected.getUuid());assertEquals(500, response.getStatus());} }整合測(cè)試
問題: REST服務(wù)器集成測(cè)試是否應(yīng)該使用實(shí)時(shí)數(shù)據(jù)庫?
答:這是一個(gè)技巧問題。 我們都需要。
總體架構(gòu)包含三個(gè)Maven模塊。 我們前面介紹了student-ws-client,今天介紹了Student-ws-server。 每個(gè)都創(chuàng)建一個(gè).jar文件。 第三個(gè)模塊-student-ws-webapp-創(chuàng)建實(shí)際的.war文件。 學(xué)生-ws-服務(wù)器模塊的集成測(cè)試應(yīng)使用虛擬服務(wù)層,而學(xué)生-ws-webapp模塊的集成測(cè)試應(yīng)使用完整堆棧。
我們從集成測(cè)試開始,該集成測(cè)試反映了客戶端模塊中的單元測(cè)試。
public class CourseRestServerIntegrationTest {CourseRestClient client = new CourseRestClientImpl("http://localhost:8080/rest/course/");@Testpublic void testGetAll() throws IOException {Course[] courses = client.getAllCourses();assertNotNull(courses);}@Test(expected = ObjectNotFoundException.class)public void testUnknownCourse() throws IOException {client.getCourse("missing");}@Testpublic void testLifecycle() throws IOException {final String physicsName = "Physics 201";final Course expected = client.createCourse(physicsName);assertEquals(physicsName, expected.getName());final Course actual1 = client.getCourse(expected.getUuid());assertEquals(physicsName, actual1.getName());final Course[] courses = client.getAllCourses();assertTrue(courses.length > 0);final String mechanicsName = "Newtonian Mechanics 201";final Course actual2 = client.updateCourse(actual1.getUuid(),mechanicsName);assertEquals(mechanicsName, actual2.getName());client.deleteCourse(actual1.getUuid());try {client.getCourse(expected.getUuid());fail("should have thrown exception");} catch (ObjectNotFoundException e) {// do nothing}} }我們還需要一個(gè)虛擬服務(wù)類,該類可以實(shí)現(xiàn)足以支持我們的集成測(cè)試的功能。
public class DummyCourseService implements CourseService {private Map cache = Collections.synchronizedMap(new HashMap<String, Course>());public List<Course> findAllCourses() {return new ArrayList(cache.values());}public Course findCourseById(Integer id) {throw new ObjectNotFoundException(null); }public Course findCourseByUuid(String uuid) {if (!cache.containsKey(uuid)) {throw new ObjectNotFoundException(uuid); }return cache.get(uuid);}public Course createCourse(String name) {Course course = new Course();course.setUuid(UUID.randomUUID().toString());course.setName(name);cache.put(course.getUuid(), course);return course;}public Course updateCourse(Course oldCourse, String name) {if (!cache.containsKey(oldCourse.getUuid())) {throw new ObjectNotFoundException(oldCourse.getUuid()); }Course course = cache.get(oldCourse.getUuid());course.setUuid(UUID.randomUUID().toString());course.setName(name);return course; }public void deleteCourse(String uuid) {if (cache.containsKey(uuid)) {cache.remove(uuid);}} }pom.xml
pom.xml文件應(yīng)包含一個(gè)用于運(yùn)行嵌入式碼頭或tomcat服務(wù)器的插件。 作為集成測(cè)試的一部分,高級(jí)用戶可以旋轉(zhuǎn)和拆除嵌入式服務(wù)器-請(qǐng)參閱更新。
<build><plugins><!-- Run the application using "mvn jetty:run" --><plugin><groupId>org.mortbay.jetty</groupId><artifactId>maven-jetty-plugin</artifactId><version>6.1.16</version> <!-- ancient! --><configuration><!-- Log to the console. --><requestLog implementation="org.mortbay.jetty.NCSARequestLog"><!-- This doesn't do anything for Jetty, but is a workaround for a Maven bug that prevents the requestLog from being set. --><append>true</append></requestLog><webAppConfig><contextPath>/</contextPath><extraClasspath>${basedir}/target/test-classes/</extraClasspath></webAppConfig></configuration></plugin></plugins> </build>更新資料
經(jīng)過更多研究之后,我進(jìn)行了配置以在集成測(cè)試期間設(shè)置和拆除碼頭服務(wù)器。 此配置使用非標(biāo)準(zhǔn)端口,因此我們無需關(guān)閉同時(shí)運(yùn)行的另一個(gè)碼頭或tomcat實(shí)例即可運(yùn)行它。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><build><plugins><!-- Run the application using "mvn jetty:run" --><plugin><groupId>org.eclipse.jetty</groupId><artifactId>jetty-maven-plugin</artifactId><version>9.1.0.v20131115</version><configuration><webApp><extraClasspath>${basedir}/target/test-classes/</extraClasspath></webApp><scanIntervalSeconds>10</scanIntervalSeconds><stopPort>18005</stopPort><stopKey>STOP</stopKey><systemProperties><systemProperty><name>jetty.port</name><value>18080</value></systemProperty></systemProperties></configuration><executions><execution><id>start-jetty</id><phase>pre-integration-test</phase><goals><goal>run</goal></goals><configuration><scanIntervalSeconds>0</scanIntervalSeconds><daemon>true</daemon></configuration></execution><execution><id>stop-jetty</id><phase>post-integration-test</phase><goals><goal>stop</goal></goals></execution></executions></plugin></plugins></build> </project>源代碼
- 可從http://code.google.com/p/invariant-properties-blog/source/browse/student/student-webservices/student-ws-server獲取源代碼。
翻譯自: https://www.javacodegeeks.com/2014/01/project-student-webservice-server-with-jersey.html
jersey put 服務(wù)
總結(jié)
以上是生活随笔為你收集整理的jersey put 服务_项目学生:带有Jersey的Web服务服务器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么提前备案异地就医(怎么提前备案)
- 下一篇: 如何在ADF中将参数传递给ActionL