Java xml响应不工作,但json正在使用Rest Api
这是我在Java xml响应不工作,但json正在使用Rest Api,java,rest,tomcat,jersey,Java,Rest,Tomcat,Jersey,这是我在restapi上使用jersey编写的第一个程序。当我试图用XML返回响应时,我的RESTAPI给了我一个错误代码500,但对于JSON来说效果很好 有人能告诉我我做错了什么吗 控制台中未显示任何错误 public class MyResource { @GET @Produces(MediaType.TEXT_PLAIN) public String getIt() { return "Got it!"; } @GET
restapi
上使用jersey编写的第一个程序。当我试图用XML返回响应时,我的RESTAPI给了我一个错误代码500
,但对于JSON来说效果很好
有人能告诉我我做错了什么吗
控制台中未显示任何错误
public class MyResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
return "Got it!";
}
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/cust")
public Customer getCust() {
Customer cus= new Customer();
cus.setName("john");
cus.setId(1);
cus.setAddress("india");
return cus;
}
@GET
@Path("/test/order")
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public Order getOrder() {
List<Order> o =new ArrayList<Order>();
Order ord=new Order();
ord.setCost(0);
ord.setProductID(0);
ord.setQuantity(0);
ord.setShippingAddress("abcd");
o.add(ord);
return ord;
}
}
我的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>in.octalian</groupId>
<artifactId>mobileservice</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mobileservice</name>
<build>
<finalName>mobileservice</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>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<!-- uncomment this to get JSON support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.27</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
4.0.0
奥克塔利安先生
问题是您试图访问URI/cus
,但您的代码有一个声明为@Path(“/cust”)
。所以您应该使用/cust
其次,您可以在调试模式下运行服务器(ApacheTomcat for me)以获取调试信息,如果控制台中没有因为某些错误而记录日志此外,您还需要在请求中添加标题“Accept”,并使用预期的结果格式类型。如果未指定,第一个将被视为第一个出现。服务器日志上是否有stacktrace?这两种资源方法都会发生吗?您可能需要尝试注册此文件。我打赌你会得到一个包含更多有用信息的堆栈跟踪,因为你还没有得到一个,是的,这两个方法都发生了。我确实注册了一个异常映射程序,但它没有显示任何堆栈跟踪。你确定映射程序已注册吗?你怎么注册的?所以现在地图绘制者没有被调用?我一定是意外按下了backspace。但是是的,我会尝试在服务器调试模式打开时检查它
<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>in.octalian</groupId>
<artifactId>mobileservice</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mobileservice</name>
<build>
<finalName>mobileservice</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>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<!-- uncomment this to get JSON support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.27</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>