我使用 JERSEY框架在我目前的应用程序中开发了一些 REST服务。 在此应用程序中,我将 RequestXML 发布到JERSEY并将 JSON 响应作为输出。
的web.xml
<servlet-name>VVCServices</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.vzw.vvc.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>VVCServices</servlet-name>
<url-pattern>/customer/*</url-pattern>
</servlet-mapping>
CustomerLookUp.java
@Path("/getCustInfo")
public class CustomerLookUp {
private static Logger logger = Logger.getLogger(CustomerLookUp.class);
@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getCustomerInfo(VVC vvc) throws Exception {
logger.info("getCustomerInfo - ENTRY");
String format="XML";
Response response = null;
com.vzw.vvc.customer.VVC.Response response1=new com.vzw.vvc.customer.VVC.Response();
VVC.Response.CustomerInfo cusInfo = new VVC.Response.CustomerInfo();
cusInfo.setEnrolled(true);
cusInfo.setEnrolledDate(Utils.getXMLGregorianCalendarNow());
cusInfo.setAccountNumber("23454");
cusInfo.setDeviceId(new Long("56566"));
cusInfo.setPlanId(new Long("4574963"));
cusInfo.setBalancePastDue(true);
response1.setCustomerInfo(cusInfo);
vvc.setResponse(response1);
ByteArrayOutputStream xml = new ByteArrayOutputStream();
JAXBContext jaxbContext = JAXBContext.newInstance(VVC.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
//output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(vvc,xml);
response = Utils.getResponse(xml,format);
logger.info("getCustomerInfo - Exit");
return response;
}
}
Utils.java
public class Utils {
static Logger logger = Logger.getLogger(Utils.class);
public static Response getResponse(ByteArrayOutputStream xmlResponse, String format) {
Response response = null;
JSONObject jsonObject;
logger.info(" in utils.java -- Entry");
try {
jsonObject = XML.toJSONObject(xmlResponse.toString());
if (format.equalsIgnoreCase("JSON")) {
response = Response.ok(jsonObject.toString(),
MediaType.APPLICATION_JSON).build();
} else {
response = Response.ok(xmlResponse.toString(),
MediaType.APPLICATION_XML).build();
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
public static XMLGregorianCalendar getXMLGregorianCalendarNow()
throws DatatypeConfigurationException
{
logger.info("IN getXMLGregorianCalendar()- Entry");
GregorianCalendar gregorianCalendar = new GregorianCalendar();
DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
XMLGregorianCalendar now =
datatypeFactory.newXMLGregorianCalendar(gregorianCalendar);
logger.info("XMLGregorianCalendar:"+now) ;
return now;
}
}
customerInfoPost.jsp 用于发布RequestXML和MIME类型..etc抛出JQUERY属性。
<input type="button" value="CustomerLookUpService" onClick="btnClick()"></input></br>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
var btnClick = function () {
var reqXml = '<VVC><ServiceHeader><ServiceName>CustomerLookup</ServiceName><Operation>testing</Operation><ClientId>11333</ClientId><UserInfo><UserId>9933</UserId><UserClass>sampletestingclass</UserClass></UserInfo></ServiceHeader><Request><SearchCondition><CustomerId>2147483647</CustomerId></SearchCondition></Request></VVC>';
var url = 'http://localhost:8080/VerizonVirtualCareServices/customer/getCustInfo';
$.ajax({
type:"POST",
url: url,
contentType: 'application/xml',
cache: false,
data: reqXml,
dataType: "xml",
success: function (data, textStatus, jqXHR) {
onCompleted(data)
},
error: function (jqXHR, textStatus, errorThrown) {
onCompleted(textStatus);
}
});
}
点击上述服务,这是JSON:
{"VVC":{"ServiceHeader":{"Operation":"testing","ServiceName":"manusample","UserInfo":{"UserClass":"sampletestingclass","UserId":"9933"},"ClientId":"11333"},"Response":{"CustomerInfo":{"DeviceId":"56566","BalancePastDue":"true","PlanId":"4574963","Enrolled":"true","AccountNumber":"23454","EnrolledDate":"2014-03-20T19:06:28.739+05:30"}},"Request":{"SearchCondition":{"CustomerId":"2147483647"}}}}
当时,为了 POC(概念验证),我应该将硬编码数据放在数据库上(oracle)并获得实际数据。
现在我想知道如何在Jersey Rest服务中使用 Spring jdbc 进行DataBase插件。
我希望对这篇文章作出回应,任何人都可以随意分享。
英语不是我的母语;请原谅输入错误
由于
玛达瓦
答案 0 :(得分:0)
你可以一起使用Spring和Jersey。然后您的资源类(在您的情况下为CustomerLookup)可以初始化为具有范围“prototype”的bean,您可以正常注入JdbcTemplate。怎么做取决于你的泽西岛版本。我在Jersey 1.8上,我就是这样做的。
根据您的示例,在web.xml中更改servlet:
<servlet>
<servlet-name>VVCServices</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.vzw.vvc.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
如果您正在使用Spring注释,请将您的注释添加到类声明中:
@Component
@Scope("prototype")
@Path("/getCustInfo")
public class CustomerLookUp {
private static Logger logger = Logger.getLogger(CustomerLookUp.class);
@Autowired
private JdbcTemplate jdbcTemplate
@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getCustomerInfo(VVC vvc) throws Exception {
... rest of your code ...
}
}
当然,有很多方法可以初始化Spring bean,所以我不知道这是否适合你当前的方法,但这就是我在我的应用程序中所做的并且它运行良好。
如果您使用的是Jersey 2.x,可以在此处找到一个示例:https://github.com/jersey/jersey/tree/2.7/examples/helloworld-spring-webapp