簡體   English   中英

春天與澤西REST框架

[英]spring with jersey REST framework

我有一個課,如下所示:

@Path("/myrequest")
@Scope("request")
@Component
public class MyRESTCode implements IServicedResource<T> {   
@Inject
private IMyService serviceImpl;

@Override
public void setServiceImpl(IMyService impl) {
    serviceImpl = impl;
}
}

@Path("/users")
@POST
@Consumes ({MediaType.APPLICATION_JSON})
@Produces ({MediaType.APPLICATION_JSON})
public Response mymethod(Object obj) throws Exception {
       serviceImpl.callme(obj);
       return Response.noContent().build();
}

現在,此callme方法實現存在於其他某些類(MyOtherClass.java)中。

當發出/ users POST請求時,任何人都可以告訴我mymethod如何在MyOtherClass.java中調用callme方法嗎?

另外,誰調用setServiceImpl方法及其設置方式以及何時調用?

謝謝!

您必須具有一個春季applicationContext.xml,其外觀應類似於

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns: xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

       <context:component-scan base-package="test.example"/>
       <context:annotation-config/>

</beans>

在此xml中,如果您注意到應將base-package定義為應存在控制器類(在您的情況下為MyRESTCode.java)的包。 Spring將搜索標注為@Component的類,並在@Path提到的路徑上對其進行配置

當您在URL .... / myrequest / users的正文中使用JSON發出發布請求時,將調用callme方法,該方法又調用您的service方法。

@Inject注釋告訴spring,應該通過setter方法setServiceImpl將IMyService的依賴項注入到serviceImpl變量中。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM