我设置的一个简单的REST调用是在运行时给我一个文件未找到的异常。我查找了该错误,并且知道该URL找不到任何东西,但是我不知道为什么。在我看来,REST Servlet可以很好地设置,但是以某种方式URL并未映射到其中实现的动作。一个类似的教程项目可以完美地工作,但即使是按照几乎完全相同的方式配置,也不能做到这一点:
输出:
{"username":"AchillesV3","password":"affirmation","email":"jason@cave-gaming.com"}
Error while calling Crunchify REST Service
java.io.FileNotFoundException: http://localhost:8080/arenamaster-backend/api/player/post
java.io.FileNotFoundException: http://localhost:8080/arenamaster-backend/api/player/post
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1890)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at RESTServiceClient.main(RESTServiceClient.java:43)
PlayerService.java
package services;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.springframework.web.bind.annotation.CrossOrigin;
import objects.*;
@Path("/player")
public class PlayerService {
@GET
@Path("/get")
@Produces(MediaType.TEXT_PLAIN)
//@CrossOrigin
public String getTestString() {
return "Get success";
}
@POST
@Path("/post")
//@CrossOrigin
@Consumes(MediaType.APPLICATION_JSON)
public Response registerNewPlayer(PlayerRegisterRequest new_player) {
String result = "Player saved : " + new_player;
return Response.status(201).entity(result).build();
}
}
其他客户:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import org.codehaus.jettison.json.JSONObject;
public class RESTServiceClient {
public static void main(String[] args) {
String string = "";
try {
// Step1: Let's 1st read file from fileSystem
// Change CrunchifyJSON.txt path here
InputStream crunchifyInputStream = new FileInputStream("JSON.txt");
InputStreamReader crunchifyReader = new InputStreamReader(crunchifyInputStream);
BufferedReader br = new BufferedReader(crunchifyReader);
String line;
while ((line = br.readLine()) != null) {
string += line + "\n";
}
JSONObject jsonObject = new JSONObject(string);
System.out.println(jsonObject);
// Step2: Now pass JSON File Data to REST Service
try {
URL url = new URL("http://localhost:8080/arenamaster-backend/api/player/post");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(jsonObject.toString());
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line_in;
while ((line_in = in.readLine()) != null) {
System.out.println(line_in);
}
System.out.println("\nCrunchify REST Service Invoked Successfully..");
in.close();
} catch (Exception e) {
System.out.println("\nError while calling Crunchify REST Service");
e.printStackTrace();
System.out.println(e);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>jersey-servlet</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>services</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:0)
尝试在Player类中进行以下更改:
@Controller
@RequestMapping("/arenamaster-backend/api/player/")
public class PlayerService {
}
然后将具有正确返回内容类型的@Produces
注释添加到registerNewPlayer
方法中,即为:
@Produces(MediaType.TEXT_PLAIN)
而不是
InputStream crunchifyInputStream = new FileInputStream("JSON.txt");
给出文件JSON.txt
的完整路径
像
InputStream crunchifyInputStream = new FileInputStream("C:\\JSON.txt");
答案 1 :(得分:0)
我通过使用Maven的命令行协议创建一个新项目来修复它。看来错误在于文件结构。