Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Resty restful framework to quickly access Spring

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use Resty restful framework to quickly access Spring". In daily operation, I believe many people have doubts about how to use Resty restful framework to quickly access Spring. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to use Resty restful framework to quickly access Spring"! Next, please follow the editor to study!

Resty has experienced nearly 10 months since its initial development, and the number of star in github is about to enter 400. without any promotion, the current situation is still considerable, mainly thanks to the people who pay attention to the development of restful.

For people who do not understand restful, it is actually a specification of url address, but I never think so. I have always thought that rest is an idea. Just as java teaches you object-oriented, rest teaches you to orient resources, not to implement interfaces in terms of functions, but to implement interfaces in the way you operate on resources. At present, as far as my own use is concerned, it is mostly a good response:

1. The interface is really extremely simple (and very), such as:

Login:

Curl-X POST http://users.api.demo.com/sessions\-H "Content-Type: application/json"\-d'{"username": "admin", "password": "111111", "rememberMe": "false"}'

Log out and login:

Curl-X DELETE http://users.api.demo.com/sessions

Get the currently logged in user:

Curl-X GET http://users.api.demo.com/sessions

Wait, the interface users find it very simple and elegant, which is convenient for management.

two。 When you open source to predict the behavior of the interface, you can do more global processing, such as in the actual situation: do permissions for a certain resource, do ETag and cache processing according to GET,POST,DELETE,PUT and other operations, GET use cache, POST,DELETE,PUT update cache, even if the interface is distributed in different projects, the cache is also real-time, and so on.

3. Facing http, you don't need to do any extra design. Http is ready for you. Come and enter the world of restful.

Snapshot version of Resty Maven

Updates for 1.1.0-SNAPSHOT:

1. Fast access to Spring

Import the package of spring:

Org.springframework spring-context ${spring-context.version}

Write the startup configuration of Spring (annotations are used by default. For students who use xml, please refer to @ ImportResource annotations), configure Spring scan paths, and support multiple

@ Configuration@ComponentScan (basePackages = {"cn.dreampie.service"}) public class HelloApp {}

Configure SpringPlugin in the AppConfig of Resty

Public void configPlugin (PluginLoader pluginLoader) {pluginLoader.add (new SpringPlugin (HelloApp.class));}

Then write your Spring class.

/ / Interface public interface HelloService {public String hello ();} @ Componentpublic class HelloServiceImpl implements HelloService {public String hello () {return "hello";}}

Using it in Resource is no different from the original Spring. You would think it is a pure Spring application, but the route part is more concise than Spring mvc.

@ Autowired private HelloService helloService; @ GET public String get () {return helloService.hello ();}

Spring integration is so simple, so perfect (what Dubbo+zookeeper is no longer a matter of).

2.resty-cache cache add expiration time, orm part uses @ Table (expired=10000)

The encryption function of 3.resty-security provides keys

Public class Encryptioner {public static String md5 (String srcStr) {return encrypt ("MD5", srcStr, null);} public static String md5 (String srcStr, String salt) {return encrypt ("MD5", srcStr, salt);} public static String sha1 (String srcStr) {return encrypt ("SHA-1", srcStr, null);} public static String sha1 (String srcStr, String salt) {return encrypt ("SHA-1", srcStr, salt) } public static String sha256 (String srcStr) {return encrypt ("SHA-256", srcStr, null);} public static String sha256 (String srcStr, String salt) {return encrypt ("SHA-256", srcStr, salt);} public static String sha384 (String srcStr) {return encrypt ("SHA-384", srcStr, null);} public static String sha384 (String srcStr, String salt) {return encrypt ("SHA-384", srcStr, salt) } public static String sha512 (String srcStr) {return encrypt ("SHA-512", srcStr, null);} public static String sha512 (String srcStr, String salt) {return encrypt ("SHA-512", srcStr, salt);}}

4. Add the Headers parameter to get all the header

@ GET ("/ headers") public Headers headers (Headers headers) {return headers;}

5. Add XForwardedSupports,add config in application.properties

App.xForwardedSupports=* or identified ip address app.xForwardedSupports=127.0.0.1127.0.0.2

6. Fix other general bug and partial code refactoring optimizations

At this point, the study on "how to use Resty restful framework to quickly access Spring" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report