加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

RESTful webService 初使用

发布时间:2020-12-17 00:09:39 所属栏目:安全 来源:网络整理
导读:RESTFul webservices 比 SOAP webservice 轻量,简单,易用。但是相对来说没有SOAP那么安全。 A RESTFul webservices are based on the HTTP methods and the concept of REST. A RESTFul webservice typically defines the base URI for the services,the s

RESTFul webservices 比 SOAP webservice 轻量,简单,易用。但是相对来说没有SOAP那么安全。


A RESTFul webservices are based on the HTTP methods and the concept of REST. A RESTFul webservice typically defines the base URI for the services,the supported MIME-types (XML,Text,JSON,user-defined,..) and the set of operations (POST,GET,PUT,DELETE) which are supported.

【摘自: http://www.vogella.com/articles/REST/article.html】


简要说明创建步骤:


创建web工程添加所需jar:

asm-3.1.jar

jersey-client-1.2.jar

jersey-core-1.2.jar

jersey-server-1.2.jar

jersey-spring-1.2.jar

jsr311-api-1.1.1.jar



web.xml 配置:

<servlet>

<servlet-name>Jersey REST Service</servlet-name>

<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

<init-param>

<param-name>com.sun.jersey.config.property.packages</param-name>

<param-value>com.test.rest</param-value> ?

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>Jersey REST Service</servlet-name>

<url-pattern>/rs/*</url-pattern>

</servlet-mapping>



txt/plain 测试:

package com.test.rest;


import javax.ws.rs.GET;

import javax.ws.rs.Path;

import javax.ws.rs.Produces;

import javax.ws.rs.core.MediaType;


@Path("/hello")

public class HelloResource {

@GET

@Produces(MediaType.TEXT_PLAIN)

public String sayHello() {

return "Hello Jersey";

}

}


访问 : http://localhost:8080/Rest/rs/hello

效果 :


html 测试:

package com.test.rest;


import javax.ws.rs.GET;

import javax.ws.rs.Path;

import javax.ws.rs.Produces;

import javax.ws.rs.core.MediaType;


@Path("/html")

public class HtmlTest {


@GET

@Produces(MediaType.TEXT_HTML)

public String getHtml() {

return "n<font color='red'>" + "Thisisaneasyresource(ashtmltext).n"

+ "</font>";

}

}


访问 : http://localhost:8080/Rest/rs/html

效果 :



xml ?测试:

package com.test.rest;


import javax.ws.rs.GET;

import javax.ws.rs.Path;

import javax.ws.rs.Produces;

import javax.ws.rs.core.MediaType;


@Path("/xml")

public class XmlTest {


// @GET

// @Produces(MediaType.TEXT_XML)

// public String getXml() {

// return "<a>" + "Thisisaneasyresource(ashtmltext)." + "</a>";

// }


@GET

@Produces( { MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON })

public Test listPageByPropertiesWithSort() {

Test map = new Test("1","111");

return map;

}

}


访问 : http://localhost:8080/Rest/rs/xml

效果 :



客户端调用 :

public static void main(String[] args) {

Client c = Client.create();

WebResource wr = c.resource("http://localhost:8080/Rest/rs/xml");

String txtRes = wr.accept(MediaType.APPLICATION_XML).get(String.class);

System.out.println("" + txtRes);

}

输出:<?xml version="1.0" encoding="UTF-8" standalone="yes"?><test><id>1</id><name>111</name></test>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读