JAX-RS(Jersey)ã§ã®Springã¨ã®é£æº
Jerseyã¨Springã¨ã®é£æºã«ã¤ãã¦ãJerseyã«ã¯ãSpringã¨ã®é£æºç¨ã®ãµã¼ãã¬ããã¯ã©ã¹ãããã®ã§ãããã使ãã¾ãã
web.xmlã«Springã¨ã®é£æºç¨ã®ãµã¼ãã¬ããã®è¨å®ã¨Springã®ãªã¹ãã¼ãè¨å®ãã¾ãã
Springã®è¨å®ã¯ãé常ã¨åãããlistenerã¨configãä¸è¨ã®æ§ã«è¨è¿°ããã ãã§ãã
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/spring/applicationContext.xml</param-value> </context-param>
ãã®å¾ã«ãJerseyã®Springã¨ã®é£æºç¨ã®ãµã¼ãã¬ãããè¨è¿°ãã¦web.xmlã®è¨å®ã¯ãå®äºã§ãã
<servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.resorceConfigClass</param-name> <param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value> </init-para> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>jp.co.dev.system.service</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> <url-pattern>/MessageResource/*</url-pattern> </servlet-mapping>
å¼ã³åºãããRESTãµã¼ãã¹ã¯ã©ã¹ã«@Componentã@Serviceãã¤ãã¾ãã
ã©ã£ã¡ã使ããã¯ãæ©è½çã«ã©ã£ã¡ã§ãåé¡ããã¾ããã
@Serviceã使ã£ãå ´åã¯ãä¸è¨ã®æ§ã«ãªãã¾ãã
package jp.co.dev.system.service; import javax.ws.rs.Get; import javax.ws.rs.Path; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import org.springframework.stereotype.Service; @Path("/service") @Service("messageResource") public class MessageResoruce { @GET @Path(value="/getMessageByMessageId") @Produces(MediaType.TEXT_XML) public MessageResponse getMessageByMessageId( .......
ããã§ãä¸è¨ã®ã¯ã©ã¹ã«å¯¾ãã¦ããã¸ãã¯ã¯ã©ã¹ãªã©ãä¸è¨ã®æ§ãªæãã§ãAutowireããããã¨ãã§ãã¾ãã
@Path("/service") @Service("messageResource") public class MessageResoruce { @Autowired @Qualifier("messageResourceLogic") MessageResourceLogic messageRsourceLogic; @GET @Path(value="/getMessageByMessageId") @Produces(MediaType.TEXT_XML) public MessageResponse getMessageByMessageId( @QueryParam("messageId") String messageId) throws ApplicationFaultException,SystemFaultException { MessageResponse response = messageResourceLogic.getMessageId(messageId); return response; }