Christie Khin
Christie Khin

Reputation: 11

JAX RS Jersey + XML --> HTTP 400 Bad Request

@Sikorski /This is my 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>

/This is the server log of tomcat. But it didn't show me any error messges./

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

/This is the updated question with the full stack trace from 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

I have one issue in sending the request xml from RestClient to Jax Rs. I accepted and saved this request with the bean which is a jaxb object. But, whenever I made a request, server returns HTTP 400 bad request.

Here is my XML request.

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

And here is my resource method

public class AddingTransResource {

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

    Trans transaction = new Trans();

The method add is accepting the XML parameter with the JAXB object, in here, which is Trans and it is also the root element of this XML.

Below is my JAXB object:

@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;
}

And the way that i'm creating the JAXB object is just creating a Java bean with @XmlRootElement.

Can anyone suggest me please how to fix it and any idea on creating the JAXB object?

Upvotes: 1

Views: 4674

Answers (3)

jdklett
jdklett

Reputation: 43

As per other suggestions add the xml header:

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

Also try adding the xmlns declaration to your root element, should be something similar to your package:

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

Upvotes: 0

metatechbe
metatechbe

Reputation: 657

You might need an XML namespace declaration (xmlns) to your root element declaration.

Otherwise, activate verbose tracing on Jersey with the following parameter in the web.xml :

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

Afterwards, send your HTTP request with the following header :

X-Jersey-Trace-Accept=true

Upvotes: 2

emgsilva
emgsilva

Reputation: 3065

This looks like some detail wrong with your XML request... the JAXB and API seem to be well defined. I would suggest you try two things:

1) add an header to the XML request, which would lead to the following 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) add specifically a name of the JAXB object in the XmlRootElement:

@XmlRootElement(name="trans")

Hope it helps. If not, provide further logging info of the exception raised (not only the HTTP Status 400 - you can get that info from the Tomcat console and log files).

Upvotes: 0

Related Questions