spring+wink rest
前面文章分享了spring集成redis、dubbo、activemq、shiro。本章分享一下在此基础上集成wink rest。
上章地址:http://www.cnblogs.com/MrLimy/p/8350982.html
首先在demo父工程的pom.xml添加wink的依赖。
<!-- wink rest --> <dependency> <groupId>org.apache.wink</groupId> <artifactId>wink-server</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>org.apache.wink</groupId> <artifactId>wink-client</artifactId> <version>1.3.0</version> </dependency> <!-- wink rest -->
打开web工程的web.xml文件,添加以下内容。
<!-- winkRest --> <servlet> <servlet-name>restSdkService</servlet-name> <servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class> <init-param> <param-name>applicationConfigLocation</param-name> <!-- 加载application文件,里面存放的是restful接口的全路径 --> <param-value>/WEB-INF/application</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>restSdkService</servlet-name> <!-- http请求的时候url以/rest开头的都会先到RestServlet --> <url-pattern>/rest/*</url-pattern> </servlet-mapping> <!-- winkRest -->
在WEB-INF下面新建名叫application的file文件。

package com.ngt.rest; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/test") public class RestfulDemo { @Path("/hello/{name}") @GET @Produces({MediaType.TEXT_PLAIN}) public String helloWorld(@PathParam("name") String name){ return "hello"+name; } }
些个控制器调用一下
package com.ngt.controller; import org.apache.wink.client.Resource; import org.apache.wink.client.RestClient; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class WinkRestController { @RequestMapping("/helloC/{name}") public void helloC(@PathVariable("name")String name){ try { RestClient restClient = new RestClient(); Resource resource = restClient.resource("http://localhost:8089/demo_web/rest/test/hello/"+name); String response = resource.contentType("text/plain;charset=UTF-8").accept("text/plain;charset=UTF-8").get(String.class); System.out.println("客户端收到服务器返回的信息: " + response); } catch (Exception e) { e.printStackTrace(); } } }
浙公网安备 33010602011771号