在本文中,我们将带你了解如何仅使用注释在这篇文章中,我们将为您详细介绍如何仅使用注释的方方面面,并解答不使用web.xml设置JAX-RSApplication?常见的疑惑,同时我们还将给您一些技巧,
在本文中,我们将带你了解如何仅使用注释在这篇文章中,我们将为您详细介绍如何仅使用注释的方方面面,并解答不使用web.xml设置JAX-RS Application?常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的Ajax: A New Approach to Web Applications、applicationContext-webService.xml 配置、applicationContext.xml的存放位置对应web.xml的写法、ApplicationListener ApplicationContextAware ApplicationEvent使用。
本文目录一览:- 如何仅使用注释(不使用web.xml)设置JAX-RS Application?
- Ajax: A New Approach to Web Applications
- applicationContext-webService.xml 配置
- applicationContext.xml的存放位置对应web.xml的写法
- ApplicationListener ApplicationContextAware ApplicationEvent使用
如何仅使用注释(不使用web.xml)设置JAX-RS Application?
前面提到的依赖项对我不起作用。从Jersey用户指南中:
Jersey提供了两个Servlet模块。第一个模块是Jersey核心Servlet模块,它提供核心Servlet集成支持,并且在任何Servlet 2.5或更高版本的容器中都是必需的:
<dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId></dependency>
为了支持其他Servlet 3.x部署模式和异步JAX-RS资源编程模型,需要一个附加的Jersey模块:
<dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId></dependency>
jersey-container-servlet
模块取决于jersey-container-servlet-core
模块,因此,在使用该模块时,不必显式声明jersey-container-servlet-core
依赖性。
答案1
小编典典接受的答案确实有效,但前提是将Web应用程序部署到Glassfish或Wildfly之类的应用程序服务器,并且可能部署到具有EE扩展名的servlet容器(例如TomEE)。它不适用于像Tomcat这样的标准servlet容器,我敢肯定,大多数在此寻找解决方案的人都想使用它。
如果你正在使用标准的Tomcat安装(或其他一些servlet容器),则需要包括REST实现,因为Tomcat并不附带此实现。如果你使用的是Maven,请将其添加到以下dependencies
部分:
<dependencies> <dependency> <groupId>org.glassfish.jersey.bundles</groupId> <artifactId>jaxrs-ri</artifactId> <version>2.13</version> </dependency> ...</dependencies>
然后只需将应用程序配置类添加到你的项目中。如果除了设置其余服务的上下文路径之外,没有其他特殊配置需求,则该类可以为空。添加此类后,你无需在web.xml其中进行任何配置(或完全不需要配置):
package com.domain.mypackage;import javax.ws.rs.ApplicationPath;import javax.ws.rs.core.Application;@ApplicationPath("rest") // set the path to REST web servicespublic class ApplicationConfig extends Application {}
之后,使用Java类中的标准JAX-RS批注直接声明Web服务:
package com.domain.mypackage;import javax.ws.rs.Consumes;import javax.ws.rs.Produces;import javax.ws.rs.GET;import javax.ws.rs.MatrixParam;import javax.ws.rs.Path;// It''s good practice to include a version number in the path so you can have// multiple versions deployed at once. That way consumers don''t need to upgrade// right away if things are working for them.@Path("calc/1.0")public class CalculatorV1_0 { @GET @Consumes("text/plain") @Produces("text/plain") @Path("addTwoNumbers") public String add(@MatrixParam("firstNumber") int n1, @MatrixParam("secondNumber") int n2) { return String.valueOf(n1 + n2); }}
这应该是你所需要的。如果你的Tomcat安装程序在端口8080上本地运行,并且将WAR文件部署到上下文中myContext,则将…
http://localhost:8080/myContext/rest/calc/1.0/addTwoNumbers;firstNumber=2;secondNumber=3
…应该产生预期的结果(5)。
Ajax: A New Approach to Web Applications
总结
以上是小编为你收集整理的Ajax: A New Approach to Web Applications全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
applicationContext-webService.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd ">
<jaxrs:server id="promotionService" address="/promotionService">
<jaxrs:serviceBeans>
<bean/>
</jaxrs:serviceBeans>
<jaxrs:inInterceptors>
<bean/>
</jaxrs:inInterceptors>
<jaxrs:outInterceptors>
<bean/>
</jaxrs:outInterceptors>
</jaxrs:server>
<jaxrs:server id="orderService" address="/orderService">
<jaxrs:serviceBeans>
<bean/>
</jaxrs:serviceBeans>
<jaxrs:inInterceptors>
<bean/>
</jaxrs:inInterceptors>
<jaxrs:outInterceptors>
<bean/>
</jaxrs:outInterceptors>
</jaxrs:server>
</beans>
applicationContext.xml的存放位置对应web.xml的写法
web.xml中classpath:和classpath*:有什么区别?
classpath:只会到你的class路径中查找找文件;
classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找.
存放位置:
1:src下面 需要在web.xml中定义如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
2:WEB-INF下面 需要在web.xml中定义如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext*.xml</param-value>
</context-param>
web.xml通过contextConfigLocation配置spring的方式 SSI框架配置文件路径问题:
struts2的1个+N个路径:src+src(可配置)名称:struts.xml+N spring的1个路径:
src名称:applicationContext.xml ibatis的1个+N个路径:
src+src(可配置)名称:sqlMapConfig.xml+N
部署到应用服务器(tomcat)后,src目录下的配置文件会和class文件一样,自动copy到应用的classes目录下
spring的配置文件在启动时,加载的是web-info目录下的applicationContext.xml,
运行时使用的是web-info/classes目录下的applicationContext.xml。
配置web.xml使这2个路径一致:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
多个配置文件的加载
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:conf/spring/applicationContext_core*.xml,
classpath*:conf/spring/applicationContext_dict*.xml,
classpath*:conf/spring/applicationContext_hibernate.xml
</param-value> </context-param>
contextConfigLocation参数定义了要装入的Spring配置文件。
首先与Spring相关的配置文件必须要以"applicationContext-"开头,
要符合约定优于配置的思想,这样在效率上和出错率上都要好很多。
还有最好把所有Spring配置文件都放在一个统一的目录下,如果项目大了还可以在该目录下分模块建目录。
这样程序看起来不会很乱。
在web.xml中的配置如下:
Xml代码
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:**/applicationContext-*.xml</param-value>
</context-param>
"**/"表示的是任意目录;
"**/applicationContext-*.xml"表示任意目录下的以"applicationContext-"开头的XML文件。
你自己可以根据需要修改。最好把所有Spring配置文件都放在一个统一的目录下,如:
<!--Spring的配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/applicationContext-*.xml</param-value>
</context-param>
ApplicationListener ApplicationContextAware ApplicationEvent使用
AppCacheContext.JAVA
方法一:implements ApplicationListener
@SuppressWarnings("rawtypes")
public class AppCacheContext implements ApplicationListener{
Logger logger = LoggerFactory.getLogger(AppCacheContext.class);
@Autowired
private static AppCacheContextDao cacheDao;
public void onApplicationEvent(ApplicationEvent event) {
Class<? extends ApplicationEvent> clazz = event.getClass();
System.out.println(clazz.getName());
if(event instanceof ContextRefreshedEvent){
ApplicationContext appcontext = ((ApplicationContextEvent) event).getApplicationContext().getParent();
if(appcontext != null){
try {
// 数据字典初始化
initDicDetailCache();
logger.info("----------------数据字典初始化完成----------------------");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
方法二:ApplicationListener<ContextRefreshedEvent>
@SuppressWarnings("rawtypes")
public class AppCacheContext implements ApplicationListener<ContextRefreshedEvent>{
Logger logger = LoggerFactory.getLogger(AppCacheContext.class);
@Autowired
private static AppCacheContextDao cacheDao;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext appcontext = event.getApplicationContext().getParent();
if(appcontext != null){
try {
// 数据字典初始化
initDicDetailCache();
logger.info("----------------数据字典初始化完成----------------------");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
在ApplicationListener<ContextRefreshedEvent>使用时,会存在一个问题,在web 项目中(spring mvc),系统会存在两个容器,一个是root application context (过程中) ,另一个就是我们自己的 projectName-servlet context(作为root application context的子容器,初始完成之后)。这种情况下,就会造成onApplicationEvent方法被执行两次。根据自己的需求使用。
上述例子是初始化缓存,在完成之后执行。
解决办法可参考:https://my.oschina.net/u/2369810/blog/682899?nocache=1501657886165
今天关于如何仅使用注释和不使用web.xml设置JAX-RS Application?的介绍到此结束,谢谢您的阅读,有关Ajax: A New Approach to Web Applications、applicationContext-webService.xml 配置、applicationContext.xml的存放位置对应web.xml的写法、ApplicationListener ApplicationContextAware ApplicationEvent使用等更多相关知识的信息可以在本站进行查询。
本文标签: