当我尝试发送消息在服务器上的响应是:不支持的媒体类型。 (415)REST发布请求
{
"street": "Alex",
"picture": "fsdfs"
}
我使用Jersey 2.7。当我打电话给GET时,它是okey,但问题在于POST方法。 到项目我包括org.glassfish.jersey.media:jersey-media-json-jackson和IL \ ncluded像this topic
public static void main(String[] args) throws Exception, RuntimeException {
final Application application = new ResourceConfig()
.packages("org.template")
.register(JacksonFeature.class);
server = new Server(PORT_NUMBER);
ServletContextHandler servletContextHandler = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
servletContextHandler.addFilter(GuiceFilter.class, "/*", EnumSet.allOf(DispatcherType.class));
servletContextHandler.addServlet(DefaultServlet.class, "/*");
server.start();
server.join();
}
简单的API代码:
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ExampleAPI {
@GET
@Path("hello")
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Hello World";
}
@POST
@Path("/create")
public Response create(BeanInfo bean) {
return Response.created(UriBuilder.fromResource(BeanInfo.class)
.path("{picture}").build(bean.getPicture()))
.entity(bean.toString())
.build();
}
}
什么是错在这里?我应该将对象映射到json吗?
的BeanInfo:
@JsonProperty("picture")
private final String picture;
@JsonProperty("street")
private final String street;
...
我的客户是“邮递员REST客户端”,所以我相信这是好 – qwerty12345
好吧,但你必须正确配置它。我使用Postman来测试我的API,所以看看这个图片,看看如何正确配置它(注意“Header”部分) - > [Image at TinyPic](http://tinypic.com/r/2dsqtg1/8) – Marlon
问题在于bean类。 – qwerty12345