http rest JAVA_使用Java创建rest 服务 通过HTTP请求访问资源

使用jersey创建rest webservice

1 在eclipse中创建动态web工程

2 build jersey jar包

3 创建rest 服务端

package com.kcharf.gis.restws;

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定义了类的层次路径

//指定了资源类提供该服务的URI路径

@Path("UserInfoService")

public class UserInfo {

//@Get表示方法会处理HTTP get请求

@GET

@Path("/name/{i}")//指定资源类提供服务的uri路径

@Produces(MediaType.TEXT_XML)//资源类方法会产生媒体类型

//PathParam向Path定义的表达式注入uri参数值

public String userName(@PathParam("i") String i){

String name = i;

return "" + "" + name + "" + "";

}

}

4 创建客户端测试

package com.kcharf.gis.restclient;

import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.Client;

import com.sun.jersey.api.client.ClientResponse;

import com.sun.jersey.api.client.WebResource;

import com.sun.jersey.api.client.config.ClientConfig;

import com.sun.jersey.api.client.config.DefaultClientConfig;

/**

*

* @author pavithra

*

*/

public class UserInfoClient {

public static final String BASE_URI = "http://localhost:8080/restws";

public static final String PATH_NAME = "/UserInfoService/name/";

public static final String PATH_AGE = "/UserInfoService/age/";

public static void main(String[] args) {

String name = "Pavithra";

int age = 25;

ClientConfig config = new DefaultClientConfig();

Client client = Client.create(config);

WebResource resource = client.resource(BASE_URI);

WebResource nameResource = resource.path("rest").path(PATH_NAME + name);

System.out.println("Client Response \n"

+ getClientResponse(nameResource));

System.out.println("Response \n" + getResponse(nameResource) + "\n\n");

WebResource ageResource = resource.path("rest").path(PATH_AGE + age);

System.out.println("Client Response \n"

+ getClientResponse(ageResource));

System.out.println("Response \n" + getResponse(ageResource));

}

/**

* 返回客户端请求。

* 例如:

* GET http://localhost:8080/restws/rest/UserInfoService/name/Pavithra

* 返回请求结果状态“200 OK”。

*

* @param service

* @return

*/

private static String getClientResponse(WebResource resource) {

return resource.accept(MediaType.TEXT_XML).get(ClientResponse.class)

.toString();

}

/**

* 返回请求结果XML

* 例如:Pavithra

*

* @param service

* @return

*/

private static String getResponse(WebResource resource) {

return resource.accept(MediaType.TEXT_XML).get(String.class);

}

5 部署web.xml到WEB-INF下

RESTfulWS

Jersey REST Service

com.sun.jersey.spi.container.servlet.ServletContainer

com.sun.jersey.config.property.packages

com.kcharf.gis.restws

1

Jersey REST Service

/rest/*

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中通过使用HttpClient库可以方便地调用HTTP接口,并且可以使用RESTful风格的方法进行调用。 要调用REST方法,首先要创建一个HttpClient对象,可以通过HttpClients类的静态方法创建默认的HttpClient对象,也可以根据需要配置HttpClient对象的参数,例如设置连接超时时间、请求超时时间等。 接下来,需要创建一个HttpRequest对象,这个对象对应着要发送的请求。通过HttpRequest对象的setMethod方法可以设置请求的方法,例如GET、POST、PUT、DELETE等。通过HttpRequest对象的setURI方法可以设置请求的URL,可以包含查询参数、路径参数等。通过HttpRequest对象的addHeader方法可以设置请求的Header信息,例如Content-Type、Authorization等。 建立了HttpClient对象和HttpRequest对象后,就可以执行请求了。可以通过HttpClient的execute方法传入HttpRequest对象来发送请求,返回一个HttpResponse对象。通过HttpResponse对象的getStatusLine方法可以获取响应的状态码、通过getEntity方法可以获取响应的内容。可以根据实际需要处理响应的内容,例如将其转换为字符串、解析为JSON对象等。 在处理完请求后,需要释放资源,可以通过HttpResponse对象的close方法来关闭响应。另外,为了防止资源泄漏,还应该在适当的地方关闭HttpClient对象。 总结来说,通过使用JavaHttpClient库可以轻松地调用REST方法。需要创建HttpClient对象和HttpRequest对象,设置请求的方法、URL、Header等信息,执行请求并处理响应,最后释放资源。这样就可以实现对REST接口的调用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值