在Eclipse中,我创建了动态Web项目,然后配置为Maven。之后,我开发了基本的Rest服务:我添加了一个User类(name,age)和一个html页面以及一个rest服务。为html页面的按钮单击ajax发送请求以停止服务。 Rest服务将用户名和年龄作为json对象返回。但是它确实适用于json。我发生了500错误,但是如果我选择文本或字符串服务成功。我无法解决它,请帮助我让我疯狂:)
我检查了有关Google Pom依赖项或Web xml conf的所有示例。但这不起作用
-- My user class
package main.java.com.json;
public class User {
private String userName;
private int userAge;
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
public void setUserAge(int userAge) {
this.userAge = userAge;
}
public int getUserAge() {
return userAge;
}
}
-- My rest service
@Path("/RestApiService")
public class RestApiService {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/printFeedback/{var1}/")
public User printFeedback(@PathParam("var1") String var1) {
User user = new User();
user.setUserAge(28);
user.setUserName("xxx yyy");
return user;
}
}
-- my html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="theme/js/jquery.js"></script>
</head>
<body>
<input maxlength="6" name="usename" id="userId">
<button id="clickBtn">Tıkla</button>
</body>
<script>
$("#clickBtn").click(function () {
var userId=$("#userId").val() ;
alert("userId" +userId);
$.ajax({
url: "./rest/RestApiService/printFeedback/"+userId+"/",
type: "GET",
dataType:"json",
success: function(response) {
alert("hello");
},
error: function() {
alert("error");
}
});
});
</script>
</html>
---web.xml----
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>RestApiExample</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>main.java.com.services</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
-----pom.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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>RestApiExample</groupId>
<artifactId>RestApiExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.6</version>
</dependency>
</dependencies>
</project>
控制台日志
我的控制台中出现500个间隔服务器错误:严重:MessageBodyWriter 找不到媒体type = application / json,type = class main.java.com.json.User,genericType = class main.java.com.json.User。