我在委托类中使用@Service带注释的JAX-RS资源时遇到问题。这是我的设置:
MyServlet.java
package com.company.mobileservice.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.HttpRequestHandler;
public class MyServlet implements HttpRequestHandler {
@Autowired
MyDelagate delegate;
private void writeResponse(HttpServletRequest request,
HttpServletResponse response, String delegateResponse) {
try {
response.setContentType("application/json");
String callback = request.getParameter("callback");
if (callback != null) {
delegateResponse = callback + "(" + delegateResponse + ");";
} else {
throw new Exception("Callback was null.");
}
response.getWriter().write(delegateResponse);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private String delegateGetRequest(HttpServletRequest request) {
return delegate.handleRequest(request);
}
@Override
public void handleRequest(HttpServletRequest request,
HttpServletResponse response) {
String delegateResponse = delegateGetRequest(request);
writeResponse(request, response, delegateResponse);
}
}
MyDelegate.java
package com.company.mobileservice.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import com.company.mobileservice.cards.CardResource;
@Component
public class MyDelagate {
@Autowired
private CardResource cardResource;
public String handleRequest(HttpServletRequest request) {
return addCard(request);
}
// Proxies the request to CardResource
private String addCard(HttpServletRequest request) {
String accountID = request.getParameter("accountID");
Card card = new Card();
card.setPan(accountInformation.getAccountNumber().toString());
card.setPaymentTypeId("ABCD1234");
Response response = cardResource.addCard(request, "TEST", null, card);
return response.toString();
}
}
CardResource.java
package com.company.mobileservice.cards;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import com.company.mobileservice.model.Card;
@Path("/{appId}/cards")
@Service
@Scope("singleton")
public class CardResource {
@POST
@Consumes({"application/xml", "application/json"})
@Produces({"application/xml", "application/json"})
public Response addCard(
@Context HttpServletRequest request,
@PathParam("appId") String appId,
@QueryParam("location") String location,
Card card) {
// Do work here
}
}
我想做的是从委托中调用我的@Service。我能够从应用程序上下文成功地将CardResource注入我的委托,但我想知道是否有另一种方法可以使用RESTful @Path方法访问此服务。
答案 0 :(得分:0)
经过一番搜索,我确定这是不可能的。我希望有一些服务目录,我可以从中获得适当的服务。最后,我决定只在localhost上调用该服务。它基本上完成了这项工作,但如果我可以在我自己的应用程序中进行REST服务调用而不必配置客户端,那肯定会很好。