Spring Practical notes: Spring integration

Source: Internet
Author: User
Tags soap serialization

I. Using remote services

A remote call is a session between the client app and the server.

1.Spring supports RPC via a variety of remote call Technologies(Remote Procedure Call, remoted procedure calls)

rpc model use scene
rmi access/Publish Java-based services
http invoker jax-rpc and Jax-ws access/publish platform standalone, SOAP-based Web service

Remote service:

-RMI: Java-based remote method invocation, using the Java serialization mechanism, the server side and the client Java version need to be consistent (http://blog.csdn.net/a19881029/article/details/9465663);

--Hessian: Similar to RMI, is the use of binary messages for client and server interaction, but Hessian binary messages can be ported to other non-Java languages (PHP, Python, C + +, C #);

--Burlap: XML-based remote invocation technology that can be ported to a language that can parse XML and is easy to read during debugging.

Hessian and Burlap are HTTP-based, which solves the problem of RMI's firewall infiltration. However, RMI should be used when passing over RPC messages that contain serialized objects.

--HTTP Invoker: is a remote invocation solution provided by the spring framework. The client and server must be spring applications and must be Java-based (serialization mechanisms).


SOA: A service-oriented architecture, a simple object Access Protocol is a simple protocol for exchanging information in a decentralized or distributed environment, and is an XML-based protocol. The core idea is that applications can and should be designed to rely on a common set of core services, rather than re-implementing the same functionality for every application.

Spring provides strong support for using Java API for XML Web service (JAX-WS) to publish and use supported SOAP Web services.

The JAX-WS programming model uses annotations to declare the methods of classes and classes as operations of a Web service. Classes annotated with the @webservice annotations are considered endpoints of the Web service, and the methods annotated with the @webmethod annotations are considered operations.

Https://www.ibm.com/developerworks/cn/xml/x-sisoap/index.html


2.REST

The difference between RPC and rest:

--RPC Remote procedure call is service oriented, focusing on behavior and action;

Rest is resource-oriented, emphasizing the things and nouns that describe the application.

REST:

--Presentation (representational): rest resources can actually be expressed in various forms, including XML, JSON, and HTML (choose any form that best suits the resource user);

State: When using rest, we pay more attention to the state of the resource than to the behavior of the resource;

-Transfer (Transfer): Rest involves transferring resource data, which is transferred from one application to another in some form of representation.

That is, rest is to transfer the state of a resource from the server side to the client (or vice versa) in the form of the most appropriate client or service side.

resources are transferred in a certain form in the network.

The use of verbs in the HTTP protocol to achieve the addition of resources, modification, deletion and other operations:

--Get: Used to get resources,

--POST: Used to create a new resource (also available for updating resources)

-PUT: Used to update resources,

--Delete: Used to delete resources.

Spring supports creating rest in the following ways:

--The controller can handle all HTTP methods, including four main rest methods: GET, PUT, DELETE, Post,patch supported in Spring 3.2 and later.

-With @pathvariable annotations, the controller is able to handle parameterized URLs (variable input as part of the URL);

-With spring's view and view parser, resources can be expressed in a variety of ways, including rendering model data as XML, JSON, Atom, and RSS view implementations;

--You can use Contentnegotiatingviewresolver to select the most appropriate client representation;

--Can replace view-based rendering with @responsebody annotations and various httpmethodconverter implementations;

--@RequestBody annotations and Httpmethodconverter implementations can talk about incoming HTTP data into Java objects that are passed into the controller processing method;

-Easy access to rest resources with resttemplate,spring applications.

Spring provides two ways to convert the Java of a resource into a representation that is sent to the client:

--Content negotiation: Select a view that renders the model as a representation of the presentation to the client;

The view parser Contentnegotiatingviewresolver and Contentnegotiationmanager work together. The response of this method is to render the model: the map of Key-value, not the resource, is not the expected result of the client.

--a Message converter (HTTP information Converter): Converts the object returned by the controller to a representation presented to the client through a message converter.

return the resource status in the response body:

Normally, the party processing method returns the Java object (except for string or view implementations), which is placed in the model and rendered in the view. When using the Message transformation feature, we need to tell spring to skip the normal model/view process and use a message converter. The simplest method is to add @responsebody annotations to the controller method.

@ResponseBody note tells Spring that we want to send the returned object as a resource to the client and convert it to a representation that the client can receive.

@RequestMapping (Method=requestmethod.get, produces= "Application/json") public @ResponseBody list<spittle>  Spittles (@RequestParam (value= "Max", defaultvalue=max_long_as_string) LONG Max, @RequestParam (value= "Count", defaultvalue= ") int count) {return spittlerepository.findspittles (max, count);}

The produces attribute is used in the @requestmapping annotation in this example to indicate that this method only processes requests that are expected to output as JSON. That is, the method only handles requests for the Accept header information that contains "Application/json". Any other type of request, even if his URL matches the specified path and is a GET request, will not be processed by this method. Such requests are handled by other methods (if appropriate), or the client HTTP 406 response is returned.

Receive resource status in the request body:

@ResponseBody can tell spring to use a message device when sending the data to the client, and, similarly, @RequestBody can tell spring to find a message converter.

@RequestMapping (Method=requestmethod.post, consumes= "Application/json")--consumes similar to produces, But it will focus on the header information of the Content-type request public @ResponseBody list<spittle> Savespittle (@RequestBody spittle spittle) {return s Pittlerepository.save (spittle);}

@ResponseBody and @requestbody are a neat and powerful way to enable message converters, with the introduction of @restcontroller annotations in Spring 4.0, If you use @restcontroller instead of @controller on the controller class, spring will apply the message transformation functionality for all of the controller's processing methods.

Send error message to client:

Spring provides several ways to handle scenarios that return error messages:

--Use @responsestatus annotations to specify status codes;

--The Controller method can return a Responseentity object that can contain more response-related metadata;

-the exception handler is able to handle the error scenario so that the processor approach can focus on the normal situation.

@RequestMapping (value= "/{id}", method=requestmethod.get) public @ResponseEntity <?> Spittlebyid (@PathVariable    Long id) {spittle spittle = Spittlerepository.findone (ID);        if (spittle = = null) {Error error = new error (4, "spittle [" + ID + "] not found");    return new Responseentity<error> (Error, Httpstatus.not_found); } return new Responseentity<error> (spittle, Httpstatus.ok);}

Handling Exceptions:

Run when the specified exception is thrown.

@ExceptionHandler (Spittlenotfoundexception.class) @ResponseStatus (httpstatus.not_found) public responseentity<    Error> Spittlenotfound (spittlenotfoundexception e) {Long Spittleid = E.getspittleid (); return new Error (4, "spittle [" + ID + "] not Found");}


3.Spring message

Synchronous communication: The client application interacts directly with the remote service and waits until the remote process is complete, just like execution.

Asynchronous message: One way an application sends a message indirectly to another application without waiting for a response from the other.

To build the message agent in spring:

--Create a connection factory;

--Statement activemq;

--Use the spring JMS template.


Spring Practical notes: Spring integration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.