Java RESTful2请求
当我同时做两个请求时,我的回报是混合的 我在浏览器上做测试 表1 表2 我怎样才能修好它? 我的代码有什么问题 returno1ws.JAVA TESTE1WS.JAVAJava RESTful2请求,java,Java,当我同时做两个请求时,我的回报是混合的 我在浏览器上做测试 表1 表2 我怎样才能修好它? 我的代码有什么问题 returno1ws.JAVA TESTE1WS.JAVA 您想知道为什么即使在无状态bean中使用synchronized,您仍然可以获得静态数据的基本随机快照?你真的需要数据是静态的吗?这只是为了测试。在我的服务器上,返回来自数据库。我使用sleep方法模拟了一个长时间的搜索。对,但潜在的问题仍然存在:您使用的是静态数据。任何时候你修改它,你都在为每个人修改它。我试图理解你为什么认
您想知道为什么即使在无状态bean中使用synchronized,您仍然可以获得静态数据的基本随机快照?你真的需要数据是静态的吗?这只是为了测试。在我的服务器上,返回来自数据库。我使用sleep方法模拟了一个长时间的搜索。对,但潜在的问题仍然存在:您使用的是静态数据。任何时候你修改它,你都在为每个人修改它。我试图理解你为什么认为它会起作用。我在问你为什么使用静态数据。现在我理解了你的问题,我将我的CodeRetrono1ws.JAVA改为非静态,一切正常。谢谢你的帮助
localhost:8089/WebApplication2/resources/teste1?rep=2&clone=2
RETURN
{"codRepresentante":2,"codRetWS":100,"msgRetWS":"WebService de teste consumido com sucesso"}
localhost:8089/WebApplication2/resources/teste1?rep=1&clone=1
RETURN
{"codRepresentante":2,"codRetWS":100,"msgRetWS":"WebService de teste consumido com sucesso"}
package base;
public final class Retorno1WS {
private static int CodRetWS;
private static int CodRepresentante;
private static String MsgRetWS;
public synchronized int getCodRetWS() {
return Retorno1WS.CodRetWS;
}
public synchronized void setCodRetWS(int CodRetWS) {
Retorno1WS.CodRetWS = CodRetWS;
}
public synchronized int getCodRepresentante() {
return Retorno1WS.CodRepresentante;
}
public synchronized void setCodRepresentante(int CodRepresentante) {
Retorno1WS.CodRepresentante = CodRepresentante;
}
public synchronized String getMsgRetWS() {
return Retorno1WS.MsgRetWS;
}
public synchronized void setMsgRetWS(String MsgRetWS) {
Retorno1WS.MsgRetWS = MsgRetWS;
}
}
package ws;
import base.Retorno1WS;
import java.sql.SQLException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.ws.rs.*;
@Stateless
@Path("/teste1")
public class Teste1WS {
/**
*
* @throws SQLException
*/
public Teste1WS() throws SQLException, Exception {
super();
}
@GET
@Produces("application/json")
public synchronized Retorno1WS getTeste(@DefaultValue("0") @QueryParam("rep") int CodRep) {
Retorno1WS RetWS1 = new Retorno1WS();
RetWS1.setCodRetWS(100);
RetWS1.setMsgRetWS("WebService de teste consumido com sucesso");
RetWS1.setCodRepresentante(CodRep);
Random rand = new Random();
int randomNum = rand.nextInt((1500 - 500) + 1) + 500;
try {
Thread.sleep(randomNum);
} catch (InterruptedException ex) {
Logger.getLogger(Teste1WS.class.getName()).log(Level.SEVERE, null, ex);
}
return RetWS1;
}
}