Java restful web services@POST(在数据库中创建用户)不支持的媒体类型错误
这部分作业我已经做了一段时间了,但是没有用。我可以让我所有的@get方法发挥作用,但不能@POST。以下是我的实现:Java restful web services@POST(在数据库中创建用户)不支持的媒体类型错误,java,web-services,http,post,curl,Java,Web Services,Http,Post,Curl,这部分作业我已经做了一段时间了,但是没有用。我可以让我所有的@get方法发挥作用,但不能@POST。以下是我的实现: @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Doctor createDoctor(Doctor doctor) throws Exception{ return dao.createDoctor(doctor); } 我的dao方法:
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Doctor createDoctor(Doctor doctor) throws Exception{
return dao.createDoctor(doctor);
}
我的dao方法:
public Doctor createDoctor(Doctor doctor) throws Exception {
PreparedStatement query = null;
Connection conn = null;
try {
conn = DatabaseConnection.getDataSource().getConnection();
query = conn
.prepareStatement("INSERT INTO doctor (dId, first_name, last_name, specialty, sex, experience, salary) VALUES (?,?,?,?,?,?,?)");
query.setInt(1, doctor.getId());
query.setString(2, doctor.getFirstName());
query.setString(3, doctor.getLastName());
query.setString(4, doctor.getSpecialty());
query.setString(5, doctor.getSex());
query.setInt(6, doctor.getExperience());
query.setDouble(7, doctor.getSalary());
query.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if (conn != null)
conn.close();
}
return doctor;
}
我正在调用的curl命令:
curl -i -X POST -H 'Content-Type:application/json' -d '{"dId":2,"first_name":"joanna","last_name":"smith","specialty":"eyebrows","sex":"female","experience":1,"salary":1.0}' http://localhost:7001/FirstRestfulService/api/medicalsystem
我得到的错误是:
HTTP/1.1 415 Unsupported Media Type
Connection: close
Date: Thu, 20 Mar 2014 05:14:17 GMT
Content-Length: 22
Content-Type: text/html; charset=UTF-8
X-Powered-By: Servlet/3.0 JSP/2.2
Unsupported Media Type
请所有医生:
@Path("/doctors")
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Doctor> getAllDoctors() throws Exception{
return dao.getAllDoctors();
}
@Path(“/doctors”)
@得到
@产生(MediaType.APPLICATION_JSON)
公共列表getAllDoctors()引发异常{
return dao.getAllDoctors();
}
让所有患者:
@Path("/patients")
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Patient> getAllPatients() throws Exception{
return dao.getAllPatients();
}
@Path(“/patients”)
@得到
@产生(MediaType.APPLICATION_JSON)
public List getAllPatients()引发异常{
返回dao.getAllPatients();
}
用户@FormParam在post方法中添加注释,然后重试
@张贴@产生(MediaType.APPLICATION_JSON)
@使用(MediaType.APPLICATION_JSON) 公共医生createDoctor(@FormParam(“医生”)医生)引发异常{
返回刀。创建医生(医生);
}希望您是从
Windows
执行请求,并且它不支持参数周围的单引号“
。改为使用双引号“
-H "Content-Type:application/json"
对于JSON对象,也从内部转义双引号。例如:
-d "{\"dId\":2,\"first_name\": ... and more...}"
还要确保json格式正确,以便服务器能够解析它
最后,在curl请求中添加
-v
参数,这样您就可以在屏幕上看到curl的调试输出,这将对您有很大帮助。您需要对JSON数据进行字符串化。Post将使用应用程序/x-www-form-urlencoded
。对不起,我如何对JSON数据进行字符串化?我看到了一些示例,它们使用了双quheader:-H“Content-Type:application/json”。试一试。谢谢。我最终认为我需要转义字符,但我不知道为什么我需要它们。你能解释一下为什么我需要在json对象的每个双引号前面加一个反斜杠吗?@user3369362,因为json
放在中。。。“
。因此,如果你不逃避这些,操作系统将很难理解数据在哪里结束!我懂了。谢谢。如果你不介意的话,我还有一个问题。我不明白为什么我的@GET方法中的一个会自动输出每个用逗号分隔的JSON对象,而我的另一个GET方法以一种难以理解的方式输出。两个代码都是相同的。请参考我编辑的原始帖子。不幸的是,我不能发布图片来准确地告诉你我在说什么。我明白你的意思。我觉得两个都一样。从这里很难猜到任何东西:(