火狐resteasy 怎么用
发布网友
发布时间:2022-04-30 03:40
我来回答
共2个回答
热心网友
时间:2023-10-09 23:47
RESTEasy是JBoss的开源项目之一,是一个RESTful Web Services框架。RESTEasy的开发者Bill Burke同时也是JAX-RS的J2EE标准制定者之一。JAX-RS是一个JCP制订的新标准,用于规范基于HTTP的RESTful Web Services的API。JAXB-XML绑定:将XML映射成成面向对象的数据类。这个过程也有J2EE标准,而RESTEasy是全面支持JAXB的。
SOA和WOA所作用的抽象层面也有所不同。前者是一种系统层面的架构模式,致力于实施可被多种应用程序消化的新业务功能;而后者则是一种接口层面的架构模式,专注于各种服务功能以何种方式提供给应用程序。
web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>javax.ws.rs.core.Application</param-name>
<param-value>com.service.ChannelApplication</param-value>
</context-param>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/1.0</param-value>
</context-param>
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/1.0/*</url-pattern>
</servlet-mapping>
</web-app>
Java类
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
public class ChannelApplication extends Application
{
HashSet<Object> singletons = new HashSet<Object>();
public ChannelApplication()
{
singletons.add(new GsmModemChannelService());
}
@Override
public Set<Class<?>> getClasses()
{
HashSet<Class<?>> set = new HashSet<Class<?>>();
return set;
}
@Override
public Set<Object> getSingletons()
{
return singletons;
}
}
Service
@Path(value="smsmessaging")
public class GsmModemChannelService {
@POST
@Path("outbound/requests")
@Proces("text/html")
public String sendMessage(
@FormParam("address") String address,
@FormParam("message") String message,
@FormParam("senderName") String senderName) {
return "test";
}
@GET
@Path("inbound/messages")
@Proces("application/json")
public List<Record> getMessages() {
return getRecordList();
}
@GET
@Path("inbound/messages/{sendAddress}/messages.html")
@Proces("text/html")
public String getMessageBySender(
@PathParam("sendAddress") String sendAddress) throws Exception {
List<Record> list = new ArrayList<Record>();
for (Record r : getRecordList()) {
if (r.getFromAddr().equals(sendAddress)) {
list.add(r);
}
}
JettisonMappedContext context = new JettisonMappedContext(RecordList.class);
StringWriter writer = new StringWriter();
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(new RecordList(list), writer);
return writer.toString();
}
}
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import com.magima.messenger.gsmmodem.job.Record;
@XmlRootElement(name="listing")
public class RecordList {
private List<Record> records;
public RecordList(){
}
@XmlElement(name="records")
public List<Record> getRecords() {
return records;
}
public RecordList(List<Record> records) {
this.records = records;
}
}
热心网友
时间:2023-10-09 23:48
可以把resteasy理解为servlet,只不过它支持rest风格的接口定义方式。jsp中form表单提交时一定要指定一个action,告诉浏览器请求服务器的地址。然后resteasy需要将该地址映射到具体类的具体方法上,该方法就会执行。