当前位置:首页 » 城管服务 » restful服务

restful服务

发布时间: 2020-12-22 16:33:10

㈠ RESTful WebService和web service的区别

restful是一种架构风格,其核心是面向资源;而webService底层SOAP协议,主要核心是面向活动。

SOAP:简单对象访问协议,很轻量,同时作为应用协议可以基于多种传输协议来传递消息(Http,SMTP等)。

客户端和服务器端的通讯方式

总结:

REST对于资源型服务接口来说很合适,同时特别适合对于效率要求很高,但是对于安全要求不高的场景。而SOAP的成熟性可以给需要提供给多开发语言的,对于安全性要求较高的接口设计带来便利。所以我觉得纯粹说什么设计模式将会占据主导地位没有什么意义,关键还是看应用场景。成熟度SOAP虽然发展到现在已经脱离了初衷,但是对于异构环境服务发布和调用,以及厂商的支持都已经达到了较为成熟的情况。不同平台,开发语言之间通过SOAP来交互的web service都能够较好的互通。

㈡ 如何在Java客户端调用RESTful服务

public static void main(String[] args) { Store store = new Store();// 准备参数来 // 首先创建一个webservice客户端自,参数依次为:webservice的url, webservice的名称, webservice的方法, 参数列表, 返回类型, 泛型的类型(不需要泛型就传入null) WebserviceClient client = new WebserviceClient(", "store", "find", new Object[] { store }, List.class, Store.class); List<Store> list = client.execute();// 调用webservice System.out.println("从服务器返回" + list.size() + "个商品");// 得到了服务端返回的数据 }

㈢ 怎么实现一个可配置的RESTful服务

DELETE方法是用来删除URL所指定的资源的,作为HTTP协议规定的方法之一,当然可以被使用,只是需要注意下面的一些细节,避免系统设计意外。
1、具体实现上,服务器可能会有自己变通的处理方式,比如出于安全、数据完整性的考虑,把`删除`转义为`禁用`,毕竟删除数据是危险的,需要意识到这一点
2、提交DELETE请求时,不能包含Entity Body。排除传输编码的要求,Entity Body实际上就是HTTP消息体:

The message-body (if any) of an HTTP message is used to carry the entity-body associated with the request or response.
The message-body differs from the entity-body only when a transfer-coding has been applied,

message-body = entity-body
| <entity-body encoded as per Transfer-Encoding>

㈣ 如何通过类对象作为方法参数的RESTful服务吗

需要在对象前声明 @ApiBodyObject
@POST

@Path("/addFavor/")
void addFavor(@QueryParam("linkId") String linkId, @ApiBodyObject User user,
@QueryParam("favorTypeCode") String favorTypeCode, @QueryParam("linkTable") String linkTable);
@ApiBodyObject 相当于 SpringMVC 的 @RequestBody
参数注解的作用就是将专 xml/json (具体那种看配置,cxf 是json) 转换为实体对象属如 User 传递给实现方法,如下:
@Override
@Transactional(readOnly = false)
public void addFavor(String linkId, User user, String favorTypeCode, String linkTable) {
}

㈤ java web 怎么对外提供restful服务

请求和响应都是基于资源表示的传输来构建的。资源是通过全局ID来标识的,这些ID一般使用的是一个统一资源标识符(URI)

㈥ 如何在Java客户端调用RESTful服务

如何在Java客户端调用RESTful服务
public static void main(String[] args) {
Store store = new Store();// 准备参数
// 首先创建一个webservice客户端,参内数依次为:webservice的url, webservice的名称, webservice的方法, 参数列表, 返回类容型, 泛型的类型(不需要泛型就传入null)
WebserviceClient client = new WebserviceClient(", "store", "find", new Object[] { store }, List.class, Store.class);
List<Store> list = client.execute();// 调用webservice
System.out.println("从服务器返回" + list.size() + "个商品");// 得到了服务端返回的数据
}

把里边链接和参数换掉

㈦ RESTful WebService和web service的区别

一个最简单web服务就一个web页面等待请求与处理。更容易理解的方式是Web
Service可以把一个应用变成一个基本WEB方式的请求与处理的应用。常见的两种
Web Service处理方式为:
a. 基于WSDL/SOAP的方式
b. Rest方式
方式a是比较正统的,客户端调用必须先取得WSDL文件,然后生成调用的API才可
以使用。它不是我要说的重点,基本调用流程如下:

方式b是Rest方式,Rest的Web Service的设计原则是基于CRUD,其支持四种操作分
别为:
GET – 获取信息/请求信息内容,绝大多数浏览器获取信息时使用该方式。
POST – 增加信息内容,显示以前的信息内容,可以看作是insert操作
PUT – 更新信息内容,相当与update
DELETE – 删除信息内容可以看作是delete

Rest方式更加简单便捷,如果从设计原则上看HTTP协议本身已经是最Restful风格的
协议了HTTP协议很好的支持了CRUD的操作。正是因为如此,WEB2.0以来, 基于
Restful的Web Service越来越多的成为首选。

二:认识RestfulStyle
Rest的全称是可表述状态迁移(RepresentationalState Transfer), 可能从字面看有点奇怪
HTTP协议本身无状态协议,其保持连接通过设置请求头字段Connection: keep-alive与
设置过期时间来同时控制。其实Rest方式的WebService也是无状态的这样做的好处最少
有以下两个:
1. 更好的负载平衡,减轻服务器端负担
2. 更快的客户端响应,减少不必要的状态检查。

Restful 风格的兴起,要感谢互联网巨头Google,Facebook等他们提供大量基于Restful
风格的web服务,从谷歌地图到天气预报到翻译,国内的互联网巨头腾讯,新浪微博也
发布自己的web服务,吸引更多的开发者加入他们的阵营。Rest除了满足基本的CRUD
设计原则之外,还要遵循如下约定:
1. 资源操作可以通过描述来实现即Representation
2. 消息本身是无状态与自我描述(传输支持XML与JSON)
3. 可以发送与接受多个Representation

Rest风格(Restful Style)架构原则:
1. 客户服务器方式
2. 无状态协议传输
3. 支持缓存
4. 统一接口定义
5. 分层系统设计
这样发布了Rest的Web服务API其改变不会影响到客户端程序与实现。如果你的系统
不能适用Rest风格的架构怎么办,重新设计一个新的架构,扩展Rest风格架构。但是
这个世界上绝大数的系统与应用要做的事情就是CRUD。

三. Rest与HTTP
上面已经提到过HTTP协议可能是最Rest风格的协议,而HTTP1.1协议设计的一个原则
就要实现Rest风格。所以毫无疑问HTTP的GET, POST, PUT, DELETE就是最好的证明
但是Rest风格是否可以应用到其它一些协议与系统设计中嘛,答案是肯定的,一个最好
的例子证明就POP3协议, POP3支持Fetch 数据记录,查询记录,更新记录与删除记录
(记录代表email)多么完美的Rest风格协议。

已经存在的HTTP协议应用:
1. 浏览器客户端(你天天上网,不是IE就是Chrome,或者其它浏览器,你懂的)
2. 即时消息通信,MSN/Skype支持
3. 各种内容管理系统
4. 博客系统与微博客户端应用。
5. 你可以来补充/?

㈧ 怎么通过url向一个restful服务发出delete请求

DELETE方法是用来删除URL所指定的资源的,作为HTTP协议规定的方法之一,当然可以被使用,只是需要注意下面的一些细节,避免系统设计意外。
1、具体实现上,服务器可能会有自己变通的处理方式,比如出于安全、数据完整性的考虑,把`删除`转义为`禁用`,毕竟删除数据是危险的,需要意识到这一点
2、提交DELETE请求时,不能包含Entity Body。排除传输编码的要求,Entity Body实际上就是HTTP消息体:

The message-body (if any) of an HTTP message is used to carry the entity-body associated with the request or response.
The message-body differs from the entity-body only when a transfer-coding has been applied,

message-body = entity-body
| <entity-body encoded as per Transfer-Encoding>

对于GET/POST/PUT方法,传递Entity Body都不会有问题,但对于DELETE方法,由于该方法传递Entity Body没有明确定义的语义,所以有些服务器实现会丢弃/忽略DELETE请求的entity body,或者拒绝该请求。参见HTTP协议草稿(http://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-22#page-28)4.3.5 DELETE章节:

A payload within a DELETE request message has no defined semantics;
sending a payload body on a DELETE request might cause some existing
implementations to reject the request.

注意(过时的)老版本的RFC2616(http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3)里面没有特别说明这一点。

3、PUT/DELETE方法从语义上来讲,对资源是有破坏性的,PUT更改现有的资源,而DELETE摧毁一个资源,带有破坏性质的方法有时候会被防火墙或者IT管理人员特别关注,所以当提供PUT/DELETE接口服务时,需要确保服务器前置的防火墙没有block这些方法。当然如果走的是HTTPS协议,无需担心这一点。

java中使HttpDelete可以发送body信息
RESTful api中用到了DELETE方法,android开发的同事遇到了问题,使用HttpDelete执行DELETE操作的时候,不能携带body信息,研究了很久之后找到了解决方法。 我们查看httpclient-4.2.3的源码可以发现,methods包下面包含HttpGet, HttpPost, HttpPut, HttpDelete等类来实现http的常用操作。 其中,HttpPost继承自,类又实现了HttpEntityEnclosingRequest接口,实现了setEntity的方法。 而HttpDelete继承自HttpRequestBase,没有实现setEntity的方法,因此无法设置HttpEntity对象。 这样解决方法就明显了,我们可以自己实现一个MyHttpDelete类,继承自,覆盖其中的getMethod方法,使其返回“DELETE”。
public class MyHttpDelete extends {

public static final String METHOD_NAME = "DELETE";

public String getMethod() {
return METHOD_NAME;
}

public MyHttpDelete(final String uri) {
super();
setURI(URI.create(uri));
}

public MyHttpDelete(final URI uri) {
super();
setURI(uri);
}

public MyHttpDelete() {
super();
}

}

使用delete方法时,直接可以按下面方式操作
DefaultHttpClient httpClient = new DefaultHttpClient();

MyHttpDelete delete = new MyHttpDelete("http://url.com");

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

delete.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpClient.execute(delete);

㈨ 如何在Java中 提供 RESTful Web 服务

通过REST风格体系架构,请求和响应都是基于资源表示的传输来构建的。资源是通过全局ID来标识的,这些ID一般使用的是一个统一资源标识符(URI)。客户端应用使用HTTP方法(如,GET、POST、PUT或DELETE)来操作一个或多个资源。通常,GET是用于获取或列出一个或多个资源,POST用于创建,PUT用于更新或替换,而DELETE则用于删除资源。
例如,GET http //host/context/employees/12345将获取ID为12345的员工的表示。这个响应表示可以是包含详细的员工信息的XML或ATOM,或者是具有更好UI的JSP/HTML页面。您看到哪种表示方式取决于服务器端实现和您的客户端请求的MIME类型。
RESTful Web Service是一个使用HTTP和REST原理实现的Web Service。通常,一个RESTful Web Service将定义基本资源URI、它所支持的表示/响应MIME,以及它所支持的操作。
本文将介绍如何使用Spring创建Java实现的服务器端RESTful Web Services。这个例子将使用浏览器、curl和Firefox插件RESTClient作为发出请求的客户端。
本文假定您是熟悉REST基本知识的。
Spring 3的REST支持
在Spring框架支持REST之前,人们会使用其他几种实现技术来创建Java RESTful Web Services,如Restlet、RestEasy和Jersey。Jersey是其中最值得注意的,它是JAX-RS(JSR 311)的参考实现。
Spring是一个得到广泛应用的Java EE框架,它在版本3以后就增加了RESTful Web Services开发的支持。虽然,对REST的支持并不是JAX-RS的一种实现,但是它具有比标准定义更多的特性。REST支持被无缝整合到Spring的MVC层,它可以很容易应用到使用Spring构建的应用中。
Spring REST支持的主要特性包括:
注释,如@RequestMapping 和 @PathVariable,支持资源标识和URL映射
支持为不同的MIME/内容类型使用不同的表示方式
使用相似的编程模型无缝地整合到原始的 MVC 层
创建一个示例RESTful Web Service
本节中的例子将演示Spring 3环境的创建过程,并创建一个可以部署到Tomcat中的“Hello World”应用。然后我们再完成一个更复杂的应用来了解Spring 3 REST支持的重要概念,如多种MIME类型表示支持和JAXB支持。另外,本文还使用一些代码片断来帮助理解这些概念。
Hello World:使用Spring 3 REST支持
要创建这个例子所使用的开发环境,您需要:
IDE:Eclipse IDE for JEE (v3.4+)
Java SE5 以上
Web 容器:Apache Tomcat 6.0(Jetty或其他容器也可)
Spring 3框架(v3.0.3是本文编写时的最新版本)
其他程序库:JAXB 2、JSTL、commons-logging
在 Eclipse 中创建一个Web应用,然后设置Tomcat 6作为它的运行环境。然后,您需要设置web.xml文件来激活Spring
WebApplicationContext。这个例子将Spring bean配置分成两个文件:rest-servlet.xml 包含与MVC/REST有关的配置,rest-context.xml包含服务级别的配置(如数据源 beans)。清单 1 显示了web.xml中的Spring配置的部分。
清单 1. 在web.xml中激活Spring WebApplicationContext

以下是引用片段:

contextConfigLocation

/WEB-INF/rest-context.xml

<!-- This listener will load other application context file in addition to
rest-servlet.xml -->

org.springframework.web.context.ContextLoaderListener

rest

org.springframework.web.servlet.DispatcherServlet

1

rest
/service/*

在rest-servlet.xml文件中创建Spring MVC的相关配置(Controller、View、View Resolver)。清单 2 显示了其中最重要的部分。

清单 2. 在rest-servlet.xml文件中创建Spring MVC配置

以下是引用片段:

<bean class="org.springframework.web.servlet.mvc.annotation
." />
<bean class="org.springframework.web.servlet.mvc.annotation
." />

<bean id="jaxbMarshaller"
class="org.springframework.oxm.jaxb.Jaxb2Marshaller">

dw.spring3.rest.bean.Employee
dw.spring3.rest.bean.EmployeeList

<bean id="employees" class=
"org.springframework.web.servlet.view.xml.MarshallingView">

<bean id="viewResolver" class=
"org.springframework.web.servlet.view.BeanNameViewResolver" />

上面的代码中:

Component-scan启用对带有Spring注释的类进行自动扫描,在实践中,它将检查控制器类中所定义的@Controller注释。
和使用@ReqeustMapping注释的类或函数的beans由Spring处理这个注释将在下一节进行详细介绍。
Jaxb2Mashaller定义使用JAXB 2进行对象XML映射(OXM)的编组器(marshaller)和解组器(unmarshaller )
MashallingView定义一个使用Jaxb2Mashaller的XML表示view
BeanNameViewResolver使用用户指定的bean名称定义一个视图解析器
本例将使用名为“employees”的MarshallingView。
这样就完成了Spring的相关配置。下一步是编写一个控制器来处理用户请求。清单3显示的是控制器类。

㈩ spring mvc 提供的restful 服务怎么访问

Spring MVC本身对支持非常好。它的@RequestMapping、@RequestParam、@PathVariable、@ResponseBody注解很好的支持了REST。18.2 Creating RESTful services
1. @RequestMapping
Spring uses the @RequestMapping method annotation to define the URI Template for the request. 类似于struts的action-mapping。 可以指定POST或者GET。
2. @PathVariable
The @PathVariable method parameter annotation is used to indicate that a method parameter should be bound to the value of a URI template variable. 用于抽取URL中的信息作为参数。(注意,不包括请求字符串,那是@RequestParam做的事情。)
@RequestMapping("/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable String ownerId, Model model) {
// ...
}

如果变量名与pathVariable名不一致,那么需要指定:
@RequestMapping("/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable("ownerId") String theOwner, Model model) {
// implementation omitted
}

Tip
method parameters that are decorated with the @PathVariable annotation can be of any simple type such as int, long, Date... Spring automatically converts to the appropriate type and throws a TypeMismatchException if the type is not correct.
3. @RequestParam
官方文档居然没有对这个注解进行说明,估计是遗漏了(真不应该啊)。这个注解跟@PathVariable功能差不多,只是参数值的来源不一样而已。它的取值来源是请求参数(querystring或者post表单字段)。
对了,因为它的来源可以是POST字段,所以它支持更丰富和复杂的类型信息。比如文件对象:
@RequestMapping("/imageUpload")
public String processImageUpload(@RequestParam("name") String name,
@RequestParam("description") String description,
@RequestParam("image") MultipartFile image) throws IOException {
this.imageDatabase.storeImage(name, image.getInputStream(),
(int) image.getSize(), description);
return "redirect:imageList";
}

还可以设置defaultValue:
@RequestMapping("/imageUpload")
public String processImageUpload(@RequestParam(value="name", defaultValue="arganzheng") String name,
@RequestParam("description") String description,
@RequestParam("image") MultipartFile image) throws IOException {
this.imageDatabase.storeImage(name, image.getInputStream(),
(int) image.getSize(), description);
return "redirect:imageList";
}

4. @RequestBody和@ResponseBody
这两个注解其实用到了Spring的一个非常灵活的设计——HttpMessageConverter 18.3.2 HTTP Message Conversion
与@RequestParam不同,@RequestBody和@ResponseBody是针对整个HTTP请求或者返回消息的。前者只是针对HTTP请求消息中的一个 name=value 键值对(名称很贴切)。
HtppMessageConverter负责将HTTP请求消息(HTTP request message)转化为对象,或者将对象转化为HTTP响应体(HTTP response body)。
public interface HttpMessageConverter<T> {

// Indicate whether the given class is supported by this converter.
boolean supports(Class<? extends T> clazz);

// Return the list of MediaType objects supported by this converter.
List<MediaType> getSupportedMediaTypes();

// Read an object of the given type form the given input message, and returns it.
T read(Class<T> clazz, HttpInputMessage inputMessage) throws IOException,
;

// Write an given object to the given output message.
void write(T t, HttpOutputMessage outputMessage) throws IOException,
;

}

Spring MVC对HttpMessageConverter有多种默认实现,基本上不需要自己再自定义HttpMessageConverter
StringHttpMessageConverter - converts strings
FormHttpMessageConverter - converts form data to/from a MultiValueMap<String, String>
ByteArrayMessageConverter - converts byte arrays
SourceHttpMessageConverter - convert to/from a javax.xml.transform.Source
- convert to/from RSS feeds
- convert to/from JSON using Jackson's ObjectMapper
etc...
然而对于RESTful应用,用的最多的当然是。
但是不是默认的HttpMessageConverter:
public class extends WebContentGenerator
implements HandlerAdapter, Ordered, BeanFactoryAware {

...

public () {
// no restriction of HTTP methods by default
super(false);

// See SPR-7316
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setWriteAcceptCharset(false);
this.messageConverters = new HttpMessageConverter[]{new ByteArrayHttpMessageConverter(), stringHttpMessageConverter,
new SourceHttpMessageConverter(), new ()};
}
}

如上:默认的HttpMessageConverter是ByteArrayHttpMessageConverter、stringHttpMessageConverter、SourceHttpMessageConverter和转换器。所以需要配置一下:
<bean class="org.springframework.web.servlet.mvc.annotation.">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=GBK</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.json." />
</list>
</property>
</bean>

配置好了之后,就可以享受@Requestbody和@ResponseBody对JONS转换的便利之处了:
@RequestMapping(value = "api", method = RequestMethod.POST)
@ResponseBody
public boolean addApi(@RequestBody
Api api, @RequestParam(value = "afterApiId", required = false)
Integer afterApiId) {
Integer id = apiMetadataService.addApi(api);
return id > 0;
}

@RequestMapping(value = "api/{apiId}", method = RequestMethod.GET)
@ResponseBody
public Api getApi(@PathVariable("apiId")
int apiId) {
return apiMetadataService.getApi(apiId, Version.primary);
}

一般情况下我们是不需要自定义HttpMessageConverter,不过对于Restful应用,有时候我们需要返回jsonp数据:
package me.arganzheng.study.springmvc.util;

import java.io.IOException;
import java.io.PrintStream;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.;
import org.springframework.http.converter.json.;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class extends {

public () {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationConfig(objectMapper.getSerializationConfig().withSerializationInclusion(Inclusion.NON_NULL));
setObjectMapper(objectMapper);
}

@Override
protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, {
String jsonpCallback = null;

RequestAttributes reqAttrs = RequestContextHolder.currentRequestAttributes();
if(reqAttrs instanceof ServletRequestAttributes){
jsonpCallback = ((ServletRequestAttributes)reqAttrs).getRequest().getParameter("jsonpCallback");
}

if(jsonpCallback != null){
new PrintStream(outputMessage.getBody()).print(jsonpCallback + "(");
}

super.writeInternal(o, outputMessage);

if(jsonpCallback != null){
new PrintStream(outputMessage.getBody()).println(");");
}
}
}

热点内容
影视转载限制分钟 发布:2024-08-19 09:13:14 浏览:319
韩国电影伤口上纹身找心里辅导 发布:2024-08-19 09:07:27 浏览:156
韩国电影集合3小时 发布:2024-08-19 08:36:11 浏览:783
有母乳场景的电影 发布:2024-08-19 08:32:55 浏览:451
我准备再看一场电影英语 发布:2024-08-19 08:14:08 浏览:996
奥迪a8电影叫什么三个女救人 发布:2024-08-19 07:56:14 浏览:513
邱淑芬风月片全部 发布:2024-08-19 07:53:22 浏览:341
善良妈妈的朋友李采潭 发布:2024-08-19 07:33:09 浏览:760
哪里还可以看查理九世 发布:2024-08-19 07:29:07 浏览:143
看电影需要多少帧数 发布:2024-08-19 07:23:14 浏览:121