ถ้าเราใช้ JavaScript การที่จะเขียนขึ้นมานั้นไม่ได้ยากเย็นอะไร ด้วยการเขียนไฟล์เพียง 1 ไฟล์ ก็สามารถทำงานได้แล้ว แต่งานที่ต้องทำตอนนี้ดันต้องใช้ Service ของ Alfresco ซึ่งเขียนด้วย Java ถ้าจะเขียนด้วย JavaScript มันดูจะวุ่นวายและเละได้เลยคิดว่าใช้ Java นี่แหละแล้วใช้ Spring Inject เข้าไป
...แล้วมันทำงานยังไงฟระ!!
คำถามนี้เกิดขึ้นมาหัวเพราะว่าตอนเขียนด้วย JavaScript เราใช้ชื่อไฟล์เป็นตัวกำหนดว่ามันจะทำงานที่ HTTP Method อะไรของ Web Service แต่ Java มันไม่ใช่แน่ๆ ระหว่างหาข้อมูลเลยกำหนดขอบเขตด้วยเทสไว้ก่อนดีกว่า
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void testGetListModelShouldBeReturnCountOfModel() throws ClientProtocolException, IOException, JSONException { | |
Response response = sendRequest(new GetRequest("/api/models/list"), 200, "admin"); | |
JSONObject actualData = new JSONObject(response.getContentAsString()); | |
assertThat(actualData.getInt("count"), Is.is(equalTo(2))); | |
} |
ตอนเขียนเทสคิดได้ว่าระบบ Audit Service ของ Alfresco มันก็ใช้ Java เหมือนกันเลยไปนั่งอ่านโค้ด มัน extends AbstractAuditWebScript ซึ่ง extends DeclarativeWebScript อีกที โอ้ว!! ได้หนทางล่ะเพราะงั้นก็จัดเลย
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ModelGet extends DeclarativeWebScript { | |
@Override | |
public Map<String, Object> executeImpl(WebScriptRequest request, Status response, Cache cache) { | |
Map<String, Object> model = new HashMap<String, Object>(); | |
model.put("count", 2); | |
return model; | |
} | |
} |
แล้วก็เขียน description และหน้า view ของ Web Service ขึ้นมาตามปกติ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<webscript> | |
<shortname>List Model</shortname> | |
<description>List Model</description> | |
<url>/api/models/list</url> | |
<authentication>user</authentication> | |
<format default="json"></format> | |
</webscript> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"count": ${count} | |
} |
แต่เดี๋ยวก่อน แล้วมันจะเชื่อมหากันได้ไงระหว่าง Web Service description กับ Java Class ที่เราเขียนไป ก็หาต่อไปอีกจนพบแสงสว่างว่าเราต้องเขียน Sping Bean ขึ้นมาเพื่อ map กับ Web Service description ของเราซะ โดยมันจะ map ชื่อ bean ของเรากับ path ของ Web Service description ให้เอง
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<bean id="webscript.models.list.get" class="webscripts.model.ListModelGet" parent="webscript"> | |
</bean> |
No comments :
Post a Comment