[英]Using HttpServletRequest in Spring Service Layer
Environment : 环境 :
Spring MVC 3 春季MVC 3
Angular JS 角JS
Tomcat 6 雄猫6
Issue : 问题 :
We are migrating a legacy application from struts2
to spring MVC REST
with angular JS as UI. 我们正在将旧版应用程序从struts2
迁移到以angular JS作为UI的spring MVC REST
。 This is a typical SPA. 这是典型的SPA。
The typical application flow is : Angular JS + Spring REST controller + Service 典型的应用程序流程是:Angular JS + Spring REST控制器+服务 layer 层
The business logic in struts2 actions is moved to service classes. struts2操作中的业务逻辑已移至服务类。
My question is - What is the correct way to make HttpServetRequest object available to Service classes in Spring ? 我的问题是-在Spring中使HttpServetRequest对象可用于Service类的正确方法是什么?
Two options available are : 可用的两个选项是:
1)Pass HttpServletRequest
to Spring MVC controller method. 1)将HttpServletRequest
传递给Spring MVC控制器方法。 Pass the same HttpServetRequest
to Spring service layer. 将相同的HttpServetRequest
传递给Spring服务层。 But this will result in HttpServletRequest
being part of Service interface like - 但这将导致HttpServletRequest
成为Service接口的一部分,例如-
public ProductDto getProductDetails(HttpServletRequest req,int productId)
Is this a correct way ? 这是正确的方法吗? Can HttpServletRequest
be part of Service interface ? HttpServletRequest
可以成为Service接口的一部分吗?
2)Use Spring provided API : 2)使用Spring提供的API:
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
Please guide on this ? 请对此进行指导?
In addition to those mentioned options, you can introduce a class or another abstraction into your service layer, then populate it with the required parts of the HttpServletRequest
and use that abstraction inside the Service Layer . 除了提到的那些选项,你可以引入一个类或另一个抽象到您的服务层,然后用所需的部分填充它HttpServletRequest
并使用服务层内部的抽象。
For example (Borrowed from Spring Data), Suppose you want to access to page
, size
and sort
and user related query strings from the HttpServletRequest
. 例如(从Spring Data借来的),假设您想从HttpServletRequest
访问page
, size
和sort
以及与用户相关的查询字符串。 Instead of passing the request to the service layer methods, you can also encapsulate those parameters into an abstraction, say, Pageable
or UserFilterCriteria
: 除了将请求传递给服务层方法之外,您还可以将这些参数封装到一个抽象中,例如Pageable
或UserFilterCriteria
:
public interface Pageable {
int page();
int size();
List<Sort> sorts();
}
Then instead of passing the HttpServletRequest
to the service layer: 然后,而不是将HttpServletRequest
传递到服务层:
public interface UsersService {
List<User> filterUsers(HttpServletRequest request);
}
You can pass those new abstractions: 您可以传递这些新的抽象:
public interface UsersService {
List<User> filterUsers(UserFilterCriteria criteria, Pageable pageable);
}
This way you can easily define abstractions in the same level of abstraction as your service layer. 这样,您可以轻松地在与服务层相同的抽象级别上定义抽象。
You should rather not make HttpServletRequest/-Response be part of your service-layer, as HttpServletRequest/-Response are classes specific to your user interface/view technology. 您宁可不要使HttpServletRequest / -Response成为服务层的一部分,因为HttpServletRequest / -Response是特定于用户界面/视图技术的类。 Introducing them to your service layer will make your service layer depend on the technology used for the user interface, thus making it harder to change the UI/View technology later. 将它们引入您的服务层将使您的服务层取决于用于用户界面的技术,因此使以后更改UI / View技术变得更加困难。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.