JAX RS Jersey + XML - > HTTP 400错误请求

时间:2013-07-12 06:49:24

标签: xml jaxb jax-rs

@Sikorski / 这是我的web.xml。 /

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" 
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_3_0.xsd">

 <display-name>Elite_Prepaid_New</display-name> 
  <servlet>
<display-name>JAX-RS REST Servlet</display-name>
<servlet-name>JAX-RS REST Servlet</servlet-name>
<servlet-class>
    com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
 <init-param>
    <param-name>com.elite_prepaid_new.users</param-name>
    <param-value>UsersResource</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS REST Servlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>UserRoles.jsp</welcome-file>
</welcome-file-list> 
</web-app>

/ 这是tomcat的服务器日志。但它没有向我显示任何错误消息。 /

This is the log file from tomcat. But it didn't show me there's any error. 127.0.0.1 - - [15/Jul/2013:11:04:12 -0700] "GET /     HTTP/1.1" 404 951
0:0:0:0:0:0:0:1 - - [15/Jul/2013:11:04:12 -0700] "GET /Elite_Prepaid_New/ HTTP/1.1" 200 936
0:0:0:0:0:0:0:1 - - [15/Jul/2013:11:11:56 -0700] "POST /Elite_Prepaid_New/services/transactions/add HTTP/1.1" 400 990
0:0:0:0:0:0:0:1 - - [15/Jul/2013:13:56:10 -0700] "POST /Elite_Prepaid_New/services/transactions/add HTTP/1.1" 400 990
0:0:0:0:0:0:0:1 - - [15/Jul/2013:13:56:36 -0700] "POST /Elite_Prepaid_New/services/transactions/add HTTP/1.1" 400 990
0:0:0:0:0:0:0:1 - - [15/Jul/2013:14:06:36 -0700] "POST /Elite_Prepaid_New/services/transactions/add HTTP/1.1" 400 990
0:0:0:0:0:0:0:1 - - [15/Jul/2013:14:07:11 -0700] "GET /Elite_Prepaid_New/ HTTP/1.1" 200 936
0:0:0:0:0:0:0:1 - - [15/Jul/2013:14:07:14 -0700] "GET /Elite_Prepaid_New/ HTTP/1.1" 200 936
0:0:0:0:0:0:0:1 - - [15/Jul/2013:14:07:14 -0700] "GET /Elite_Prepaid_New/ HTTP/1.1" 200 936
0:0:0:0:0:0:0:1 - - [15/Jul/2013:14:07:14 -0700] "GET /Elite_Prepaid_New/ HTTP/1.1" 200 936
0:0:0:0:0:0:0:1 - - [15/Jul/2013:14:07:59 -0700] "POST /Elite_Prepaid_New/services/transactions/add HTTP/1.1" 400 990

/ 这是来自tomcat /

的完整堆栈跟踪的更新问题
Jul 15, 2013 11:04:03 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in     production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.7.0_09\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre7/bin/client;C:/Program Files/Java/jre7/bin;C:/Program Files/Java/jre7/lib/i386;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\nodejs;C:\Program Files\Java\jdk1.7.0_09\bin;C:\Users\Web Developer\AppData\Roaming\npm\;C:\Users\Web Developer\Desktop\eclipse;;.
Jul 15, 2013 11:04:04 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Elite_Prepaid_New' did not find a matching property.
Jul 15, 2013 11:04:04 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Restdemo' did not find a matching property.
Jul 15, 2013 11:04:04 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jul 15, 2013 11:04:04 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Jul 15, 2013 11:04:04 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1275 ms
Jul 15, 2013 11:04:04 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jul 15, 2013 11:04:04 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.41
Jul 15, 2013 11:04:08 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:com:sun:jersey:api:view is already defined
Jul 15, 2013 11:04:08 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:com:sun:jersey:api:view is already defined
Jul 15, 2013 11:04:08 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:com:sun:jersey:api:view is already defined
Jul 15, 2013 11:04:08 AM com.sun.jersey.api.core.servlet.WebAppResourceConfig init
INFO: Scanning for root resource and provider classes in the Web app resource paths:
  /WEB-INF/lib
  /WEB-INF/classes
Jul 15, 2013 11:04:08 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class com.myeclipseide.ws.CustomersResource
Jul 15, 2013 11:04:08 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Provider classes found:
  class org.codehaus.jackson.jaxrs.JacksonJsonProvider
  class org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider
  class org.codehaus.jackson.jaxrs.JsonParseExceptionMapper
  class org.codehaus.jackson.jaxrs.JsonMappingExceptionMapper
Jul 15, 2013 11:04:08 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.14 09/09/2012 07:21 PM'
Jul 15, 2013 11:04:10 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:com:sun:jersey:api:view is already defined
Jul 15, 2013 11:04:10 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:com:sun:jersey:api:view is already defined
Jul 15, 2013 11:04:10 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:com:sun:jersey:api:view is already defined
Jul 15, 2013 11:04:10 AM com.sun.jersey.api.core.servlet.WebAppResourceConfig init
INFO: Scanning for root resource and provider classes in the Web app resource paths:
/WEB-INF/lib

 /WEB-INF/classes
Jul 15, 2013 11:04:08 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class com.myeclipseide.ws.CustomersResource
Jul 15, 2013 11:04:08 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Provider classes found:
  class org.codehaus.jackson.jaxrs.JacksonJsonProvider
  class org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider
  class org.codehaus.jackson.jaxrs.JsonParseExceptionMapper
  class org.codehaus.jackson.jaxrs.JsonMappingExceptionMapper
Jul 15, 2013 11:04:08 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.14 09/09/2012 07:21 PM'
Jul 15, 2013 11:04:10 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:com:sun:jersey:api:view is already defined
Jul 15, 2013 11:04:10 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:com:sun:jersey:api:view is already defined
Jul 15, 2013 11:04:10 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:com:sun:jersey:api:view is already defined
Jul 15, 2013 11:04:10 AM com.sun.jersey.api.core.servlet.WebAppResourceConfig init
INFO: Scanning for root resource and provider classes in the Web app resource paths:
  /WEB-INF/lib
  /WEB-INF/classes
Jul 15, 2013 11:04:11 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class com.elite_prepaid_new.users.UsersResource
  class com.elite_prepaid_new.orders.OrdersResource
  class com.elite_prepaid_new.user_roles.UserRolesResource
  class com.elite_prepaid_new.items.ItemsResource
  class com.elite_prepaid_new.live.AddingTransResource
  class com.elite_prepaid_new.items.types.ItemsTypesResource
Jul 15, 2013 11:04:11 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Provider classes found:
  class org.codehaus.jackson.jaxrs.JacksonJsonProvider
  class org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider
  class org.codehaus.jackson.jaxrs.JsonMappingExceptionMapper
  class org.codehaus.jackson.jaxrs.JsonParseExceptionMapper
Jul 15, 2013 11:04:11 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.14 09/09/2012 07:21 PM'
Jul 15, 2013 11:04:11 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jul 15, 2013 11:04:11 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jul 15, 2013 11:04:11 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 7094 ms

我有一个问题是将请求xml从RestClient发送到Jax Rs。 我接受并使用作为jaxb对象的bean保存了此请求。 但是,每当我发出请求时,服务器都会返回HTTP 400错误请求。

这是我的XML请求。

<trans>
  <rechargephone>0990909</rechargephone>
  <phonetype>GSM</phonetype>
  <amount>10000</amount>
  <useracctid>10100100</useracctid>
  <userpin>111</userpin>
  <quantity>1</quantity>
</trans>

这是我的资源方法

public class AddingTransResource {

@POST
@Path("add")
@Consumes("application/xml")
public Message add(Trans trans) {

    Trans transaction = new Trans();

方法add接受带有JAXB对象的XML参数,在此处为Trans,它也是此XML的根元素。

下面是我的JAXB对象:

@XmlRootElement
public class Trans {

private String rechargephone;
private String phonetype;
private String amount;
private String useracctid;
private String userpin;
private int quantity;

public String getRechargephone() {
    return rechargephone;
}
public void setRechargephone(String rechargephone) {
    this.rechargephone = rechargephone;
}

我创建JAXB对象的方式就是用@XmlRootElement创建一个Java bean。

有人可以建议我如何修复它以及创建JAXB对象的任何想法吗?

3 个答案:

答案 0 :(得分:2)

您可能需要一个XML命名空间声明(xmlns)到根元素声明。

否则,使用web.xml中的以下参数激活Jersey上的详细跟踪:

<init-param>
  <param-name>com.sun.jersey.config.feature.TracePerRequest</param-name>
  <param-value>true</param-value>      
</init-param>

然后,使用以下标头发送您的HTTP请求:

X-Jersey-Trace-Accept=true

答案 1 :(得分:0)

这看起来像你的XML请求有些细节错误...... JAXB和API似乎定义得很好。我建议你尝试两件事:

1)在XML请求中添加一个标头,这将导致以下XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<trans>
<rechargephone>0990909</rechargephone>
<phonetype>GSM</phonetype>
<amount>10000</amount>
<useracctid>10100100</useracctid>
<userpin>111</userpin>
<quantity>1</quantity>
</trans>

2)在XmlRootElement中专门添加JAXB对象的名称:

@XmlRootElement(name="trans")

希望它有所帮助。如果没有,请提供所引发异常的进一步日志信息(不仅是HTTP状态400 - 您可以从Tomcat控制台和日志文件中获取该信息)。

答案 2 :(得分:0)

根据其他建议添加xml标题:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

另外,尝试将xmlns声明添加到根元素中,应该与您的包类似:

<trans xmlns="http://www.xsd.your/Package">
...
</trans>