当我在客户端使用 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();
}
}
这是客户端提出的请求。它是在古代计算机上的 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
So you need to send header with name Content-type and value application/json instead of text/plain
我按照@user7294900的建议发送了内容类型为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
You want your client to send JSON but the backend expects an HTML form (@FormParam), which is sent as media type application/x-www-form-urlencoded.
之后,我像这样更改了我的服务类(使用 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。
最佳答案
确保您的项目中有此依赖项
compile group: 'org.glassfish.jersey.media', name: 'jersey-media-json-jackson', version: '2.27'
关于java - 使用 Jersey 进行 POST 时出现错误 415,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48609008/