当我在 tomcat 服务器上运行我的 Web 应用程序时出现此错误。
这是我的代码
@Path("/robot")
public class Robot {
@POST
//@Path("/add")
@Consumes(MediaType.APPLICATION_JSON)
//@Produces(MediaType.APPLICATION_JSON)
public Response displayTemperature(ProcessModel tempData){
System.out.println(" \n\n ");
System.out.println( "|The temp is : "+ tempData.getTemperature() +" "+ " Robot deployment time is " + " "+tempData.getTime() +" |");
Client client = Client.create();
WebResource webResource2 = client.resource("http://localhost:8080/RESTServer/rest/hello/run");
ClientResponse response2 = webResource2.get(ClientResponse.class);
if (response2.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response2.getStatus());
}
String output2 = response2.getEntity(String.class);
System.out.println("\n<<<============ POSTING command to Robot Info Model ============>>>");
System.out.println(output2);
return Response.ok(200).header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS").entity("POSTED").build();
}
}
最佳答案
使用 maven 并在所需的依赖项中构建您的项目。
如前所述,根据使用的版本添加 jersey 客户端依赖:
http://mvnrepository.com/artifact/com.sun.jersey/jersey-client
1.19 版本示例:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
将之前的 XML 代码添加到您的 pom.xml 文件中
对于类路径解析,并且仅当您使用 maven 时,请查看此链接: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
提示:当您不知道什么类路径 scope 与 maven 一起使用时,请使用 compile 。但我建议你理解这个基本概念
关于java - 我的 REST 程序出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34484596/