1

@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.

任何人都可以建议我如何解决它以及创建 JAXB 对象的任何想法吗?

4

3 回答 3

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
于 2013-08-20T12:03:45.193 回答
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 控制台和日志文件中获取该信息)。

于 2013-07-12T09:50:52.123 回答
0

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

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

还尝试将 xmlns 声明添加到您的根元素,应该类似于您的包:

<trans xmlns="http://www.xsd.your/Package">
...
</trans>
于 2017-05-10T10:37:53.807 回答