• JAX-RS开发 hello world


    1.建立maven webapp工程aty-rest。

    2. 在pom文件增加spring框架、jax-rs接口、CXF实现

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <dependency>  
    2.     <groupId>javax.ws.rs</groupId>  
    3.     <artifactId>javax.ws.rs-api</artifactId>  
    4.     <version>2.0</version>  
    5. </dependency>  
    6.   
    7. <dependency>  
    8.     <groupId>org.springframework</groupId>  
    9.     <artifactId>spring-web</artifactId>  
    10.     <version>3.1.1.RELEASE</version>  
    11. </dependency>  
    12.   
    13. <dependency>  
    14.     <groupId>org.apache.cxf</groupId>  
    15.     <artifactId>cxf-rt-frontend-jaxrs</artifactId>  
    16.     <version>3.0.0</version>  
    17. </dependency>  

    3.编写rest接口和实现类

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. public interface INameService  
    2. {  
    3.     @GET  
    4.     @Path("/welcome/")  
    5.     @Produces(MediaType.APPLICATION_JSON)  
    6.     public String welcome();  
    7. }  
    8.   
    9. //   
    10. @Component("nameServiceImpl")  
    11. public class NameServiceImpl implements INameService  
    12. {  
    13.     public String welcome()  
    14.     {  
    15.         return "{"name":123}";  
    16.     }  
    17. }  

    4.web.xml中启动sping和cxf

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <!-- Spring -->  
    2.     <context-param>  
    3.         <param-name>contextConfigLocation</param-name>  
    4.         <param-value>classpath:spring.xml</param-value>  
    5.     </context-param>  
    6.     <listener>  
    7.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    8.     </listener>  
    9.   
    10.     <!-- CXF -->  
    11.     <servlet>  
    12.         <servlet-name>cxf</servlet-name>  
    13.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
    14.     </servlet>  
    15.     <servlet-mapping>  
    16.         <servlet-name>cxf</servlet-name>  
    17.         <url-pattern>/rest/*</url-pattern>  
    18.     </servlet-mapping>  

    4.配置cxf-spring.xml,并在spring.xml中将其包含进去

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.     xmlns:jaxrs="http://cxf.apache.org/jaxrs"  
    5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
    6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    7.     http://cxf.apache.org/jaxrs  
    8.     http://cxf.apache.org/schemas/jaxrs.xsd">  
    9.   
    10.     <jaxrs:server address="/greet">  
    11.         <jaxrs:serviceBeans>  
    12.             <ref bean="nameServiceImpl"/>  
    13.         </jaxrs:serviceBeans>  
    14.     </jaxrs:server>  
    15.       
    16. </beans>  


    5.用maven打包,将war部署到tomcat下。

    一切正常即可通过http://127.0.0.1:8080/aty-rest/rest/greet/welcome访问我们发布的rest服务。

  • 相关阅读:
    Spring Boot 配置文件 bootstrap vs application 到底有什么区别?
    一份完整的 Java 成神路线图,值得收藏!
    12 岁开始学编程,17 岁总结了 7 个重要教训!
    想成为顶尖 Java 程序员?先过了下面这些问题!
    Dubbo面试20问!这些题你都遇到过吗?
    yum安装出现No package crontabs available解决办法
    表结构设计方法
    后端token认证模板
    vue 用户登录 路由拦截 vuex cookie
    pyharm无法安装包的问题
  • 原文地址:https://www.cnblogs.com/qiumingcheng/p/5855346.html
Copyright © 2020-2023  润新知