我正在以邮递方式将Json格式的POST请求发送到以eclipse开发的API,但是却收到HTTP Status 500 – Internal Server Error。抛出的异常消息是:javax.servlet.ServletException:javax.ws.rs.ProcessingException:从实体流反序列化对象时出错。 我的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>jerseydemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>jerseydemo</name>
<build>
<finalName>jerseydemo</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.8</source>
<target>1.8</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>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
</dependencies>
<properties>
<jersey.version>2.31</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
我正在使用第二个功能(createcity)创建新数据:
package com.example.jerseydemo;
import java.util.List;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Path("cities")
public class CityResource {
CityRepository cities = new CityRepository();
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<City> getCity() {
return cities.getAllCity();
}
@POST
@Path("addcity")
@Consumes(MediaType.APPLICATION_JSON)
public City createcity(City city) {
cities.create(city);
return city;
}
}
我用于存储数据的CityRepository函数是:
package com.example.jerseydemo;
import java.util.*;
public class CityRepository {
List<City> cities;
public CityRepository() {
cities = new ArrayList<>();
City city1 = new City(101, "Delhi");
City city2 = new City(102, "Bombay");
City city3 = new City(103, "Patna");
cities.addAll(Arrays.asList(city1,city2,city3));
}
public List<City> getAllCity(){
return cities;
}
public void create(City city) {
// TODO Auto-generated method stub
cities.add(city);
}
}
我的GET请求功能运行正常。我是API开发的新手,所以现在很难找出错误并加以解决。请帮忙。