我目前正在学习REST,在调用方法时遇到错误。我确信它一定是一个简单的配置设置,但由于我对学习 REST 绝对陌生,因此无法弄清楚。
网络.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.mishra.dev.rahul.messenger</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
绒球.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.mishra.dev.rahul</groupId>
<artifactId>messenger</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>messenger</name>
<build>
<finalName>messenger</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<!-- uncomment this to get JSON support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
</dependencies>
<properties>
<jersey.version>2.26-b03</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
资源类:
package org.mishra.dev.rahul.messenger.resources;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("messages")
public class MessageResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public Person getMessages() {
return new Person("Rahul", 31);
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Name: " + name + ", Age: " + age;
}
}
调用时遇到错误:http://localhost:8080/messenger/webapi/messages
错误:
Jun 09, 2017 7:19:01 PM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE: MessageBodyWriter not found for media type=text/plain, type=class org.mishra.dev.rahul.messenger.resources.Person, genericType=class org.mishra.dev.rahul.messenger.resources.Person.
现在,如果我不是返回Person
对象,而是返回像 "Hello World"
这样的普通字符串,我不会遇到任何问题。
编辑:**解决方案:**我在 Person 类中缺少默认构造函数。添加默认构造函数后,我现在可以返回 Person 对象的字符串格式。
但是话虽如此,如果我更改@Produces(MediaType.APPLICATION_JSON)
并返回return new Person("Rahul", 31)
,我只会看到一个空的JSON。我不确定为什么我看不到 JSON 格式的 Person 对象。
好吧,我也面临着同样的问题,在这种情况下,对我有用的事情是我用这个注释了返回响应的 API
@Produces(MediaType.APPLICATION_JSON)
您不需要不必要地定义资源库,因为它会破坏对象的可变性。
我阅读了 Jersey 的文档,发现我需要在我的资源类中进行两项更改:
- 声明默认构造函数
- 定义实例变量的 getter 和 setter。
-
在 pom 中添加以下依赖项.xml
<dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> </dependency>
现在,在进行这些更改后,我可以查看 Person 对象的字符串和 JSON 格式。