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

Example Analysis of SpringMVC support for RESTfull in java

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

Share

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

This article mainly introduces the example analysis of java's SpringMVC support for RESTfull. The article is very detailed and has certain reference value. Friends who are interested must read it!

The details are as follows.

RESTful architecture is a popular Internet software architecture at present. It is clear in structure, up to the standard, easy to understand and easy to expand, so it is being adopted by more and more websites. The RESTful architecture standardizes url. What does it look like to write url in RESTful format? The url we usually request is similar to this:

Http://...../xxx.action?id=001&type=aaa

What does REST's url style look like? Generally speaking, it is similar to:

Http://..../xxx/001

So REST has an obvious feature: it makes url concise and passes parameters to the server through url. Springmvc also supports this REST-style url. Let's define a controller to test it:

/ / query product information, output json, use RESTful@RequestMapping ("/ itemsView/ {id}") public @ ResponseBody ItemsCustom itemsView (@ PathVariable ("id") Integer id) throws Exception {ItemsCustom itemsCustom = itemsService.findItemsById (id); return itemsCustom;}

@ ResponseBody is the annotation used to convert itemsCustom to json, while the @ PathVariable annotation is related to REST. {id} in @ RequestMapping (value= "/ itemsView/ {id}") indicates a placeholder, so the value passed in it will be passed to the parameter marked by @ PathVariable. If the parameter is the same as the variable in the placeholder, you can no longer specify it in the comment, otherwise you will specify the variable in the placeholder (that is, id). In this way, the parameters can be passed to the parameters through url.

However, this is not enough, and you need to configure REST in the front controller as follows:

Springmvc_rest org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring/springmvc.xml springmvc_rest /

It intercepts all url (/). This configuration does not conflict with the previously configured front-end controller and can coexist. After this configuration, you can type http://localhost:8080/SpringMVC_Study/itemsView/1 into the browser to test the data returned to the browser, and you can see that a string of json data is returned.

But there is a problem. After using the above configuration, all url will be blocked, so static resources will also be intercepted, so DispatcherServlet will also parse static resources, but there will be an error, so we need to set it not to parse static resources. Such as:

If there are other static resources, you should also set them up so that you don't have to parse the static resources, just access them directly when you access them.

The above is all the contents of the article "sample Analysis of java's SpringMVC support for RESTfull". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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