当我在客户端使用POST时,收到以下错误。
HTTP Status 415 - Unsupported Media Type
Type: Status Report
Message: Unsupported Media Type
Description: The origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource.
我在很多帖子后都试图解决问题。我希望我没有使用不兼容的解决方案。
GET部分工作正常,我使用Gson转换为JSON。
最初我的代码并没有从应用程序扩展,但它是另一篇文章的解决方案的一部分,他们注册了Jackson以获得JSON支持。
这是我的服务类的代码:
package com.tutorialspoint;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
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.Application;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.glassfish.jersey.jackson.JacksonFeature;
import com.google.gson.Gson;
@Path("/UserService")
public class UserService extends Application
{
UserDao userDao = new UserDao();
@Override
public Set<Class<?>> getClasses()
{
Set<Class<?>> classes = new HashSet<>();
classes.add(JacksonFeature.class);
return classes;
}
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_JSON)
public Response getUsers()
{
List<User> userList = userDao.getAllUsers();
Response.Status responseStatus = Response.Status.ACCEPTED;
return getJSONResponse(responseStatus, userList);
}
@POST
@Path("/users")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response createUser
(
@FormParam("id") int id,
@FormParam("name") String name,
@FormParam("profession") String profession,
@Context HttpServletResponse servletResponse) throws IOException
{
User user = new User(id, name, profession);
boolean userAdded = userDao.addUser(user);
if(userAdded)
{
return getJSONResponse(Status.CREATED, id);
}
//Should be changed to an error response later.
return getJSONResponse(Status.CREATED, id);
}
public static Response getJSONResponse(Response.Status status, Object entity)
{
String json = new Gson().toJson(entity);
return Response.status(status).entity(json).header("Access-Control-Allow-Origin", "*").build();
}
}
这是客户提出的要求。它是在cient计算机上使用linux中的nc命令捕获的。
POST /UserManagement/rest/UserService/users HTTP/1.1
Host: localhost:58585
Connection: keep-alive
Content-Length: 47
Accept: application/json
Origin: http://localhost:4200
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36
content-type: text/plain
Referer: http://localhost:4200/persons
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
{"id":1,"name":"Mahesh","profession":"Teacher"}
提前感谢。
<小时/> 编辑01
因此,您需要发送名称为Content-type和value application / json而不是text / plain
的标头
我按照@ user7294900的建议发送了带有content-type:application / json的请求。 (我无法通过Angular 2中的客户端正确发送,所以我使用telnet代替)
POST /UserManagement/rest/UserService/users HTTP/1.1
Host: localhost:58585
Connection: keep-alive
Content-Length: 47
Accept: application/json
Origin: http://localhost:4200
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36
content-type: application/json
Referer: http://localhost:4200/persons
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
{"id":1,"name":"Mahesh","profession":"Teacher"}
之后我得到了@braunpet
所述的错误500您希望您的客户端发送JSON,但后端需要一个HTML表单(@FormParam),该表单作为媒体类型application / x-www-form-urlencoded发送。
之后我改变了我的Service类(使用User作为createUser的参数):
package com.tutorialspoint;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
//import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
//import javax.ws.rs.FormParam;
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.Application;
//import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.glassfish.jersey.jackson.JacksonFeature;
import com.google.gson.Gson;
@Path("/UserService")
public class UserService extends Application
{
UserDao userDao = new UserDao();
@Override
public Set<Class<?>> getClasses()
{
Set<Class<?>> classes = new HashSet<>();
classes.add(JacksonFeature.class);
return classes;
}
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_JSON)
public Response getUsers()
{
List<User> userList = userDao.getAllUsers();
Response.Status responseStatus = Response.Status.ACCEPTED;
return getJSONResponse(responseStatus, userList);
}
@POST
@Path("/users")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response createUser
(
User user
// @FormParam("id") int id,
// @FormParam("name") String name,
// @FormParam("profession") String profession,
// @Context HttpServletResponse servletResponse
) throws IOException
{
// User user = new User(id, name, profession);
boolean userAdded = userDao.addUser(user);
if(userAdded)
{
// return getJSONResponse(Status.CREATED, id);
return getJSONResponse(Status.CREATED, user.getId());
}
//Should be changed to an error response later.
// return getJSONResponse(Status.CREATED, id);
return getJSONResponse(Status.CREATED, user.getId());
}
public static Response getJSONResponse(Response.Status status, Object entity)
{
String json = new Gson().toJson(entity);
return Response.status(status).entity(json).header("Access-Control-Allow-Origin", "*").build();
}
}
然后回到错误415.
答案 0 :(得分:1)
由于代码中包含以下定义,您应声明发送json内容类型:
for imagePath in imagePaths:
faceImg=Image.open(imagePath).convert('L')
faceNP = np.array(faceImg,'uint8')
ID=int(os.path.split(imagePath[-1]).split('.')[1])
#ID1 = (char, count)
#ID=int(ID1.split('.')[1])
faces.append(faceImg)
IDs.append(ID)
cv2.imshow('training',faceNP)
cv2.waitKey(10)
return IDs, faces
因此,您需要发送名称为 @Consumes(MediaType.APPLICATION_JSON)
且值为Content-type
的标头,而不是application/json
答案 1 :(得分:0)
确保您的项目中具有此依赖项
编译组:“ org.glassfish.jersey.media”,名称: 'jersey-media-json-jackson',版本:'2.27'