我无法找到正确的格式以便在POST后将响应发送回.JSP页面。首先,如何获得Web服务对客户的响应? 第二个问题是如何从Servlet调用客户端。如何使用Hibernate和Jersey在REST服务中进行POST
0
A
回答
0
因为第二部分非常简单(在适当的doGet,doPost方法中在servlet中创建类实例),我将关注第一个问题。
片段在服务器端:
import java.math.BigInteger;
import java.util.List;
import java.util.logging.Logger;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.hibernate.SessionFactory;
// internal beans and classes
import com.deepam.util.BasicUtils;
import entities.CustomerRest;
import entities.DualInteger;
import entities.Dualloc;
import model.CustomerModel;
import model.DualModel;
import model.HibernateUtil;
@Path("/customer")
public class CustomerRestWS {
private final static Logger LOGGER = Logger.getLogger(CustomerRestWS.class.getName());
private CustomerModel cm = new CustomerModel();
private DualModel dm = new DualModel();
private final String CUSTSEQ = "customer_rest_seq";
SessionFactory sessionFactory;
/** Constructor
*/
public CustomerRestWS() {
super();
LOGGER.info("***" + LOGGER.getName());
sessionFactory = HibernateUtil.getSessionFactory();
}
...
@GET
@Path("/findcustseq")
@Produces(MediaType.APPLICATION_XML)
public DualInteger selectCustSeq() {
return cm.selectCustSeqNative(CUSTSEQ);
}
// post method how to save customer into DB
@POST
@Path("/create")
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_JSON) // JSON is used for clearity between Consumes/Produces on the Client side
public Response create(final CustomerRest cust) throws JSONException {
Response response;
LOGGER.info("***" + LOGGER.getName() + " Insert Customer, id, name, last name: " + cust.getId() + ", " + cust.getFirstName() + ", " + cust.getLastName());
try {
cm.create(cust);
}
catch (Exception ex) {
// internal error
LOGGER.info("***" + LOGGER.getName() + " Exception: " + ex.getMessage());
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(Response.Status.INTERNAL_SERVER_ERROR.toString()).build();
return response;
}
// created
response = Response.status(Response.Status.CREATED)
.entity(Response.Status.CREATED.toString()).build();
return response;
}
...
在客户端:
import java.text.MessageFormat;
import java.util.logging.Logger;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.DefaultClientConfig;
// internal beans
import entities.Customer;
import entities.DualInteger;
import entities.ListCustomers;
public class CustomerRestfulClient {
private final static Logger LOGGER = Logger.getLogger(CustomerRestfulClient.class.getName());
private WebResource webresource;
private Client client;
private static final String BASE_URI = "http://localhost:8080/RestfulOracleServer/rest/";
public CustomerRestfulClient() {
// init client
client = Client.create(new DefaultClientConfig());
// init webresource
webresource = client.resource(BASE_URI).path("customer");
}
...
/** method getCustIdXML for obtaining unique ID (from sequence) */
public DualInteger getCustIdXML() throws UniformInterfaceException {
WebResource resource = webresource.path(MessageFormat.format("findcustseq", new Object[] {}));
return resource.accept(MediaType.APPLICATION_XML).get(DualInteger.class);
}
/** method saveCustXML call other method to obtain unique ID, than save Bean to DB */
public ClientResponse saveCustXML(String firstName, String lastName) throws UniformInterfaceException {
DualInteger custId = getCustIdXML();
LOGGER.info("Seqence number: " + (custId.getSeq()));
Customer cust = new Customer(custId.getSeq(), firstName, lastName);
ClientResponse response = webresource.path("create").
accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_XML).post(ClientResponse.class, cust);
LOGGER.info("Entity: " + response.getStatus());
return response;
}
注意服务器端的类Response
和客户端的ClientResponse
。看看如何在服务器端处理@Consumes
,@Produces
注释以及如何在客户端处理accept
,type
方法。有我的错误来源。 在用于.jsp的servlet控制器中,只需为WS创建Client即可。 custClient = new CustomerRestfulClient();
在构造函数中使用明显的方法doGet
,doPost
。该Servlet有自己的Request
,Response
不同于WS的Request
,Response
。在MVC模型中谨慎,Controller被服务器视为Singleton。在并发环境中,您必须保持会话连续性。 (最简单的方法是在方法中使用局部变量,当它被指示时。)相似主题的链接: Is it ok by REST to return content after POST? RESTful Java Client with POST method
相关问题
- 1. 如何使用jersey和java在REST web服务中进行身份验证
- 2. 如何使用Jersey读取REST式服务的POST参数?
- 3. 如何在Jersey REST服务中模拟builder.type()。post()方法
- 4. 发布POST JSON到Jersey REST服务
- 5. REST Web服务中的POST HTTP请求的进程参数(JERSEY)
- 6. Jersey服务使用HIbernate默认数据集进行初始化
- 7. 我如何在Jersey REST Web服务中使用Google OAuth2
- 8. 如何在Java Jersey REST服务中强制使用queryparams?
- 9. 使用Spring和Hibernate的REST Web服务
- 10. 如何使Jersey Rest POST请求同步
- 11. WCF Rest服务在2003上使用POST/JSON进行托管
- 12. https4s如何进行POST调用REST Web服务
- 13. 使用CORS跨域REST/Jersey web服务
- 14. 在运行Jersey REST服务的Grizzly服务器中使用过滤器
- 15. 如何在Jersey REST服务方法参数上使用jackson mixin
- 16. GAE Jersey RESTful服务POST JSON
- 17. 如何使用Post ..在cakephp中编写REST Web服务?
- 18. 如何使用Jersey Rest服务提供.jad文件?
- 19. Jersey Rest服务何时启动/关闭?
- 20. 使用Locust.io进行REST Web服务
- 21. 使用SAS进行REST服务
- 22. 我如何引导Jersey REST服务服务器?
- 23. 在Jersey REST服务中如何存储会话ID
- 24. 使用Java/Jersey作为REST和SOAP工作的Web服务
- 25. 使用ORMLite在REST风格的Web服务中进行事务
- 26. Jersey UniformInterfaceException试图代理到REST POST服务
- 27. 如何将Json请求作为Json对象在Jersey Rest服务中使用
- 28. 运行来自Intellij与Maven,Jersey和Tomcat的REST服务8
- 29. 使用POST Rest服务下载Excel
- 30. 使用Jersey在Java REST风格的Web服务中生成JSON和XML响应