RESTEasy搭建

1.所需要引入的依赖

一种是自己导包,一种是用集成的包

第一种,需要配置web.xml

<dependency>
  <groupId>org.jboss.resteasy</groupId>
  <artifactId>resteasy-jaxrs</artifactId>
  <version>2.2.1.GA</version>
</dependency>
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.16.10</version>
</dependency>
<dependency>
  <groupId>org.jboss.resteasy</groupId>
  <artifactId>jaxrs-api</artifactId>
  <version>2.1.0.GA</version>
  <scope>compile</scope>
</dependency>

第二种,可以不用配置web.xml

<dependencies>
    <dependency>
      <groupId>com.paypal.springboot</groupId>
      <artifactId>resteasy-spring-boot-starter</artifactId>
      <version>2.3.0-RELEASE</version>
    </dependency>
</dependencies>

 

2.web.xml配置,资源注册资源有三种方式

手动注册,手动指定,自动扫描

 

开始时,自动扫描一直无法启动,网上查到是jdk和tomcat版本问题,用的jdk1.8和tomcat7,换成tomcat8之后,完美运行。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>

  <display-name>Archetype Created Web Application</display-name>
   <!--方法3: 使用自动Scan的模式,必须启动ResteasyBootstrap监听 -->
  <context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
  </context-param>
  <!--方法3: 自动扫描结束-->
  <!--方法2: 手动注册开始-->
  <!--<context-param>-->
     <!--<param-name>resteasy.resources</param-name>-->
     <!--<param-value>com.RestApplication</param-value>-->
  <!--</context-param>-->
  <!--方法2: 手动注册结束-->

  <!-- 对应<servlet-mapping>的url,必须设置prefix做初始过滤,访问例子:http://localhost:8080/wang/service/userservice/users -->
  <context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/resteasy</param-value>
  </context-param>

  <!-- 启动服务监听器,初始化resteasy核心组件 -->
  <listener>
    <listener-class>
      org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
    </listener-class>
  </listener>

  <!-- url服务请求的分发控制器 -->
  <servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
    <!--方法1  手动指定Application  start-->
    <!--<init-param>-->
      <!--<param-name>javax.ws.rs.Application</param-name>-->
      <!--<param-value>com.RestApplication</param-value>-->
    <!--</init-param>-->
    <!--方法1  手动指定Application  end-->
  </servlet>
  <servlet-mapping>
    <servlet-name>resteasy-servlet</servlet-name>
    <url-pattern>/resteasy/*</url-pattern>
  </servlet-mapping>

</web-app>
        

3.配置application

package com;

import javax.ws.rs.ApplicationPath;
//import java.util.HashSet;
//import java.util.Set;

@ApplicationPath("/resteasy")//手动指定时不需要配置
public class RestApplication extends javax.ws.rs.core.Application{
//    -------------- 手动指定需要自己添加资源----------------
//    private Set<Object> singletons = new HashSet<Object>();
//    /*在这里增加资源类*/
//    public RestApplication () {
//        singletons.add(new service());
//    }
//    @Override
//    public Set<Object> getSingletons() {
//        return singletons;
//    }
//     -------------- 手动指定需要自己添加资源----------------
}
package com;
import org.jboss.resteasy.annotations.Form;

import javax.ws.rs.*;

@Path("/ser")
public class service {


    @GET
    @Path("/hello")
    @Produces("application/json")
    public String getHello(){
        return "hello,this is a get request";
    }

    @GET
    @Path("/{id}")
    @Produces("application/json")
    public String pathParam(@PathParam("id") String id){
        return "pathParam is  " + id;
    }


    @GET
    @Path("/query")
    @Produces("application/json")
    public String queryParam(@QueryParam("name") String name){
        return "queryParam is " + name;
    }

    @GET
    @Path("/test/{id}")
    @Produces("application/json")
    public String test(@QueryParam("name") String name,@PathParam("id") String id){
        return "queryParam is " + name + ";PathParam is " + id;
    }

    @GET
    @Path("/login")
    public String test(){
        return "login.jsp";
    }

    @POST
    @Path("/post")
    @Produces("application/json")
    public String post(@Form Emp emp){

        return emp.getName();
    }

    @PUT
    @Path("/put")
    @Produces("application/json")
    public String put(@Form Emp emp){

        return emp.getName();
    }

    @DELETE
    @Path("/remove/{id}")
    @Produces("application/json")
    public String remove(@PathParam("id") String id){

        return id+"";
    }

}
package com;

import javax.ws.rs.FormParam;

public class Emp {

    @FormParam("name")
    String name;
    @FormParam("age")
    int age;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Emp(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Emp() {
    }

    @Override
    public String toString() {
        return "Emp{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

4.前台页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>欢迎登录</title>
</head>
<%--<script type="text/javascript" src="jquery/jquery.js"></script>--%>
<script src="jquery/jquery-1.10.2.js"></script>
<body>
    welcome to you!
    <button onclick="hello()">hello</button>
    <button onclick="get()">hello</button>
    <button onclick="post()">hello</button>
    <button onclick="put()">hello</button>
    <button onclick="remove()">hello</button>
    <span id="span"></span>
</body>
<script>
    function hello(){
        var xmlhttp;
        if (window.XMLHttpRequest)
        {
            //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            // IE6, IE5 浏览器执行代码
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("span").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","/resteasy/ser/hello",true);
        xmlhttp.send();
    }

    function get(){
        $.get("/resteasy/ser/hello",function(data){
            debugger
            alert(data);
        },'text');
    }
    function post(){
        $.post("/resteasy/ser/post",{name:'tom',age:12},function(result){
            $("span").html(result);
        },'text');
    }
    function put(){
        $.ajax({
            url:"/resteasy/ser/put",
            type:"PUT",
            // contentType:"application/json",//设置请求参数类型为json字符串
            data:{name:'tom',age:12},//将json对象转换成json字符串发送
            dataType:"text",
            success:function(result){
                alert(result);
            },

        });
    }

    function remove(){
        $.ajax({
            url:"/resteasy/ser/remove/12",
            type:"DELETE",
            // contentType:"application/json",//设置请求参数类型为json字符串
            dataType:"text",
            success:function(result){
                alert(result);
            },

        });
    }

</script>
</html>

 

以上是第一种自己导入依赖,下面介绍第二种

 

<dependency>
      <groupId>com.paypal.springboot</groupId>
      <artifactId>resteasy-spring-boot-starter</artifactId>
      <version>2.3.0-RELEASE</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
</web-app>
        
package com;

import org.springframework.stereotype.Component;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@Component
@ApplicationPath("/v1")
public class ApplicationPathImpl extends Application {

}
package com;

import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Component
@Path("/province")
public class ExternalFacadeImpl{

    /**
     * http://localhost:8080/v1/province/city/1
     * @param id
     * @return
     */
    @Path("/city/{id}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String getCity(@PathParam("id") Long id) {
        return id+"";
    }
}

可以正常访问

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值