I'm testing out Jersey and I can't seem to figure out why I get a 405 Method Not Allowed when I call PUT:
#Singleton #Path("images")
public class ImageResource {
private static final String IMAGE_ID_PATH_PARAM = "{imageId : [A-Za-z0-9_\\-]+}";
private static final String EXTENSION_PATH_PARAM = "{extension : (\\.[A-Za-z]+)?}";
#GET #Path(IMAGE_ID_PATH_PARAM + EXTENSION_PATH_PARAM)
#Produces("image/*")
public Response getImage(#PathParam("imageId") String imageId,
#PathParam("extension") String extension, #QueryParam("mods") String mods) {
...
}
#PUT #Path(IMAGE_ID_PATH_PARAM)
#Consumes("image/*")
public Response putImage(#PathParam("imageId") String imageId, File image) {
...
}
}
PUT only works if I set the #GET path to #Path(IMAGE_ID_PATH_PARAM). When I add the extension part, I am getting a 405 status code. GET seems to work in both cases. Here's the output from a failed PUT:
$ curl -v --header "Content-Type: image/jpeg" --upload-file /Users/andy/Desktop/test.jpg http://localhost:9090/images/abcde
* About to connect() to localhost port 9090 (#0)
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 9090 (#0)
> PUT /images/abcde HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host: localhost:9090
> Accept: */*
> Content-Type: image/jpeg
> Content-Length: 48198
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
< HTTP/1.1 405 Method Not Allowed
< Content-Type: text/html; charset=iso-8859-1
< Date: Mon, 20 Aug 2012 18:35:59 GMT
< Allow: GET,OPTIONS,HEAD
< Transfer-Encoding: chunked
I've also tried testing without the #Produces and #Consumes annotations and it did not work either.
Let's take a look at what happens when you send the request.
Situation 1: no extension
Your methods look like this:
#GET #Path(IMAGE_ID_PATH_PARAM)
#Produces("image/*")
public Response getImage(#PathParam("imageId") String imageId,
#PathParam("extension") String extension, #QueryParam("mods") String mods) {
...
}
#PUT #Path(IMAGE_ID_PATH_PARAM)
#Consumes("image/*")
public Response putImage(#PathParam("imageId") String imageId, File image) {
...
}
When you send the following request:
PUT http://localhost:9090/images/abcde
first of all, Jersey looks for resources with the URI in question:
http://localhost:9090/images/abcde
After the resource is found, it checks what methods can be used to access it.
In this case, you have a single resource with the path defined by IMAGE_ID_PATH_PARAM. This resource can be accessed by either GET or PUT requests. Just like you specified with the annotations.
Situation 2: extension added to getImage
You methods now look like this:
#GET #Path(IMAGE_ID_PATH_PARAM + EXTENSION_PATH_PARAM)
#Produces("image/*")
public Response getImage(#PathParam("imageId") String imageId,
#PathParam("extension") String extension, #QueryParam("mods") String mods) {
...
}
#PUT #Path(IMAGE_ID_PATH_PARAM)
#Consumes("image/*")
public Response putImage(#PathParam("imageId") String imageId, File image) {
...
}
Again, you send the same request:
PUT http://localhost:9090/images/abcde
And yet again, Jersey finds the first resource matching the URL. The resource is represented by your getImage method, just like the first time. This time again, the #GET annotation does not match your request. And just like before, Jersey tries to find another method available for the resource, in order to match your request.
This time, however, no such method is found so it returns a 405.
The reason why this happens is that the methods getImage and putImage now represent different resources. If you look carefully, the paths can be read like this (I'll omit the regex for clarity):
#Path({imageId}{extension}) for getImage
and
#Path({imageId}) for putImage
While these two paths, considering the regex, can become the same thing, Jersey still sees them as identifiers of separate resources.
If you take a look at the WADL (feel free to look here if you're not familiar with the standard) file generated by Jersey (it should be available at http://localhost:9090/application.wadl), you'll notice that this is exactly what's happening.
<application xmlns="http://research.sun.com/wadl/2006/10">
<resources base="http://localhost:9090/">
<resource path="images">
<resource path="{imageId : [A-Za-z0-9_\-]+}">
<param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="imageId" style="template" type="xs:string"/>
<method id="putImage" name="PUT">
<request>
<representation mediaType="image/*"/>
</request>
<response>
<representation mediaType="*/*"/>
</response>
</method>
</resource>
<resource path="{imageId : [A-Za-z0-9_\-]+}{extension : (\.[A-Za-z]+)?}">
<param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="extension" style="template" type="xs:string"/>
<param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="imageId" style="template" type="xs:string"/>
<method id="getImage" name="GET">
<request>
<param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="mods" style="query" type="xs:string"/>
</request>
<response>
<representation mediaType="image/*"/>
</response>
</method>
</resource>
</resource>
</resources>
</application>
Notice how images has two, separate sub-resources, each one with a single method.
Solution
Adding the EXTENSION_PATH_PARAM segment to putImage's #Path annotation causes these two methods to be mapped to a single resource again, so the problem disappears. Since the regex makes this part optional, you are free to omit it and pretend it doesn't exist.
The difference can be clearly seen in the generated WADL.
<application xmlns="http://research.sun.com/wadl/2006/10">
<resources base="http://localhost:9090/">
<resource path="images">
<resource path="{imageId : [A-Za-z0-9_\-]+}{extension : (\.[A-Za-z]+)?}">
<param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="extension" style="template" type="xs:string"/>
<param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="imageId" style="template" type="xs:string"/>
<method id="putImage" name="PUT">
<request>
<representation mediaType="image/*"/>
</request>
<response>
<representation mediaType="*/*"/>
</response>
</method>
<method id="getImage" name="GET">
<request>
<param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="mods" style="query" type="xs:string"/>
</request>
<response>
<representation mediaType="image/*"/>
</response>
</method>
</resource>
</resource>
</resources>
</application>
In this case, images has exactly one sub-resource, which in turn has two available methods.
Personally, I find the automatic generation of WADL a fantastic feature of Jersey. It's a great way to see what's happening with your resource methods without spending too much time with curl or other REST client.
Related
I've been attempting to get a Spring Boot Application to generate the following SOAP Request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:api="http://simpro4.wirelesslogic.com/api/">
<soapenv:Header/>
<soapenv:Body>
<api:getAvailableCustomerSolutions>
<GetAvailableCustomerSolutionsRequest>
<accountNumbers>
<item>xxx</item>
</accountNumbers>
</GetAvailableCustomerSolutionsRequest>
</api:getAvailableCustomerSolutions>
</soapenv:Body>
</soapenv:Envelope>
And instead when using the following code:
public class WirelessLogicClient extends WebServiceGatewaySupport {
private String endpoint = "http://simprosb.wirelesslogic.com/sandbox.php/api/customerV3";
public GetAvailableCustomerSolutionsResponse getAvailableCustomerSolutionsRequest(){
GetAvailableCustomerSolutionsRequest request = new GetAvailableCustomerSolutionsRequest();
// request params
ArrayOfString accountNumbers = new ArrayOfString();
accountNumbers.getItems().add("xxx");
// build request
request.setAccountNumbers(accountNumbers);
// send request
return (GetAvailableCustomerSolutionsResponse) getWebServiceTemplate().marshalSendAndReceive(endpoint, request, new SoapActionCallback("http://simprosb.wirelesslogic.com/sandbox.php/api/customerV3/"));
}
However when the object is marshalled, using wireshark to see the actual XML sent over the wire from the spring application, I end up with the following:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<api:GetAvailableCustomerSolutionsRequest xmlns:api="http://simpro4.wirelesslogic.com/api/">
<accountNumbers>
<item>786708</item>
</accountNumbers>
</api:GetAvailableCustomerSolutionsRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
The SOAP endpoint is rather fussy, and requires 2 changes to make this work, the namespace xmlns:api="http://simpro4.wirelesslogic.com/api/" HAS to be in the element, it will not parse a response without it. Secondly, the body contents need to be wrapped in an <api:getAvailableCustomerSolutions>, which I thought that it would have been able to resolve.
I've used a chrome plugin (Boomerang) to test the endpoint without Java, and it generates sensible requests, but I've had no end of trouble trying to get this working in Java.
I don't have to use Jax-ws, I'm open to using anything that has a maven repo; I don't want to use handlers, as this is simply one SOAP operation of many I'd have to do this for, but I'm having the same issue across all the methods I've tried thus far.
The following are the relevant bits from the WSDL:
<message name="getAvailableCustomerSolutionsRequest">
<part name="getAvailableCustomerSolutionsRequest" type="tns:GetAvailableCustomerSolutionsRequest"/>
</message>
<message name="getAvailableCustomerSolutionsResponse">
<part name="return" type="tns:GetAvailableCustomerSolutionsResponse"/>
</message>
.....
<operation name="getAvailableCustomerSolutions">
<input message="tns:getAvailableCustomerSolutionsRequest"/>
<output message="tns:getAvailableCustomerSolutionsResponse"/>
</operation>
.....
<xsd:complexType name="GetAvailableCustomerSolutionsRequest">
<xsd:all>
<xsd:element name="accountNumbers" type="tns:ArrayOfString" nillable="true"/>
</xsd:all>
</xsd:complexType>
.....
<xsd:complexType name="ArrayOfString">
<xsd:sequence>
<xsd:element name="item" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
Pom.xml
<!-- tag::wsdl[] -->
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.3</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaLanguage>WSDL</schemaLanguage>
<schemaDirectory>src/main/resources/schemas</schemaDirectory>
<schemaIncludes>
<includes>wirelesslogicsandbox.xml</includes>
</schemaIncludes>-->
<generatePackage>wirelesslogic.wsdl</generatePackage>
</configuration>
</plugin>
What Exactly i need to do is to have a web service method in a REST API written in Apache CXF to accept a request like the following (preferably specifying a custom object as a parameter type)
{
"action":"read",
"resource:"new resource"
}
For now my method can do the same but it would expect a JSON string as the request body. But I need the service to describe the request parameters to the client. Meaning that in the wadl definition it should show the exact parameters that should be sent from the client. The ideal definition would be something similar to
<resource path="by-attrib">
<method name="GET">
<request>
<param name="Accept" style="header" type="xs:string"/>
<param name="Auth_Type" style="header" type="xs:string"/>
<param name="Authorization" style="header" type="xs:string"/>
<representation mediaType="application/json">
<param name="action" style="plain" type="xs:string"/>
<param name="resource" style="plain" type="xs:string"/>
</representation>
</request>
<response>
<representation mediaType="application/json">
<param name="result" style="plain" type="xs:string"/>
</representation>
</response>
</method>
</resource>
Is this possible with CXF?
Please note that using #FormParam is not what I need, if I use form params, I get issues in sending a request using XML to the same method
Thank You
Example with CXF and jackson
The service interface (use POST, not GET)
#POST
#Path("/yourservice")
#Consumes({ MediaType.APPLICATION_JSON})
#Produces({
MediaType.APPLICATION_JSON,
MediaType.APPLICATION_XML})
public Result postYourService(YourData data) throws WebApplicationException;
The service impl (nothing special)
public Result postYourService(YourData data){
Result r = new Result();
r.setResult("result");
return r;
}
The data objects (Using jaxb to simplify encoding/decoding of json or xml)
#XmlRootElement(name = "YourData")
public class YourData {
private String action;
private String resource;
//getter & setters
}
#XmlRootElement(name = "Result")
public class Result {
private String result;
//getter & setters
}
The spring configuration of CXF server and jackson. Jackson provider class depends on which version of CXF are you using. Therefore if the JacksonJaxbJsonProvider is not on your cxf package, look at the documentation
<jaxrs:server id="yourServiceREST" address="/services">
<jaxrs:serviceBeans>
<ref bean="yourService" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<!--<ref bean="authenticationFilter" />-->
<!--<ref bean="exceptionMapper" />-->
<!-- <ref bean="corsFilter" /> -->
<ref bean="jackson" />
</jaxrs:providers>
</jaxrs:server>
<bean id="yourService" class="YourServiceImpl">
</bean>
<bean id="jackson" class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider" />
Try to deploy and invoke to
POST /services/yourservice
{ "action":"read", "resource:"new resource"}
I do not know if WADL is going to be well generated because sometimes CXF fails. Be lucky!
actually I found the answer
It is to use bean injection
described in their documentation itself, sorry for not RTFM
http://cxf.apache.org/docs/jax-rs-basics.html
under Parameter Beans
Basic idea is to use a java Bean (zero argument constructor with setters and getters) and add it to the web service parameters
You however need to specify one of #QueryParam #FormParam #PathParam for it to work
I've written a custom filter for Undertow/EAP7, and have dropped it in the server config file. My XML looks something like this:
<subsystem xmlns="urn:jboss:domain:undertow:3.0">
<server name="default-server">
<http-listener name="default" socket-binding="http" redirect-socket="https"/>
<host name="default-host" alias="localhost">
.
.
<filter-ref name="myFilter" />
</host>
</server>
.
.
<filters>
.
.
<filter name="myFilter" module="josh.example.myFilter" class-name="josh.example.myFilter">
</filter>
</filters>
</subsystem>
How do I:
Provide parameters to this filter in the XML configuration
Consume those parameters in Java code
I've searched the docs a bit, and the undertow handler author's guide is silent on the matter, as well as the JBoss EAP 7 web server config guide.
Update:
Just found the .xsd, parameters can be added to the XML like this:
<filter name="myFilter" module="josh.example.myFilter" class-name="josh.example.myFilter">
<param name="foo" value="bar" />
</filter>
However, still looking to see how I can consume these on the Java handler side.
Okay, so I figured out a solution after perusing the source + a bit of trial and error. Here's what worked for me:
Per the widlfly-undertow_3_0.xsd spec (current at time of writing), you can add an arbitrary number of parameters like this:
<filter name="MyFilter" module="josh.example.MyFilter" class-name="josh.example.MyFilter">
<param name="foo" value="bar" />
<param name="magicNumber" value="7" />
</filter>
Then, create some bean-like setters matching the parameter names on your filter:
public class MyFilter implements HttpHandler {
public void setFoo(String foo) {
log.info("set foo to {}", foo);
}
public void setMagicNumber(Integer magicNumber) {
log.info("set magicNumber to {}", magicNumber);
}
}
I also attempted to use the fields as constructor parameters like so:
public class MyFilter implements HttpHandler {
public MyFilter(HttpHandler next, String foo, Integer magicNumber) {
// do stuff
}
}
However, when configured in the JBoss server configuration file (typically something like standalone.xml), the ConfiguredHandlerWrapper always grabs the HttpHandler constructor and ignores the rest. As such, this approach will not work on EAP7.
I have a C# client which produces the .NET SOAP envelope below, which works against a C# ASMX SOAP web service. However we have a Java Client calling into our service which is producing the Java envelope specified below. The main difference between the envelopes is that some values are serialised as attributes in the java client envelope rather than as XML element nodes in the C# client envelope. The java client is using AXIS WSDL2Java to generate their client. Would anyone know what I would need to tell the Java developers so they may generate the correct soap envelope for the example shown.
Kind Regards
Working SOAP Envelope Captured from C# Client
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<getDocumentPageRequest xmlns="urn:mycorp-com:MyApp.Schema.DocumentEnquiry.Messages.v01">
<header xmlns="urn:mycorp-com:MyApp.Schema.Common.Types.v01">
<extensions />
<corelationIdentifier>41edebfb-fffd-44f8-94e9-be043e1dad48</corelationIdentifier>
</header>
<securityToken xmlns="urn:mycorp-com:MyApp.Schema.Common.Types.v01">
<Value>218FD85D</Value>
</securityToken>
<documentIdentifier>15236HDFG000005</documentIdentifier>
<pageNumber>1</pageNumber>
</getDocumentPageRequest>
</soap:Body>
</soap:Envelope>
Java SOAP Envelope - Not working with web-service
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<getDocumentPageRequest
documentIdentifier="15236HDFG000005"
pageNumber="1"
xmlns="urn:mycorp-com:MyApp.Schema.DocumentEnquiry.Messages.v01">
<ns1:header corelationIdentifier="" xmlns:ns1="urn:mycorp-com:MyApp.Schema.Common.Types.v01">
<ns1:extensions/>
</ns1:header>
<ns2:securityToken xmlns:ns2="urn:mycorp-com:MyApp.Schema.Common.Types.v01">218FD85D</ns2:securityToken>
</getDocumentPageRequest>
</soapenv:Body>
</soapenv:Envelope>
Edited:: Added in WSDL as requested.
Sample WSDL and XSD Extract
Below is a sample of the WSDL generated and an extract of the XSD that it imports for the message type. I can see in this that the XML has attributes, which is what the AXIS WSDL2Java is generating, but the C# proxy and web-service is expecting XML nodes. I think this means the way the C# services is implemented is different somehow or other than the schema it is defined against. This is confusing...
<!-- WSDL Extract -->
<message name="getDocumentPageIn">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
<part name="messagePart" element="import0:getDocumentPageRequest" />
</message>
<message name="getDocumentPageOut">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
<part name="messagePart" element="import0:getDocumentPageResponse" />
</message>
<!-- import0 XSD extract -->
<xs:element name="getDocumentPageRequest">
<xs:complexType>
<xs:complexContent>
<xs:extension base="MyApp:request">
<xs:attribute name="documentIdentifier" type="xs:string" use="required"/>
<xs:attribute name="pageNumber" type="xs:short" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
Thanks for taking the time to review this, but I found the actual problem.
It turns out the C# code that was being generated for the types for the web-service and proxy was missing an [XmlAttribute] attribute in the code for each attribute defined in the XML. This caused the XmlSerializer to flatten the properties on the class to elements rather than keeping them as attributes.
I went through the documentation, which helped me figure out several issues I had, but not the following two problems:
1) I have got a get(GET) method: get(#Context Request request, #PathParam("isbn")String isbn)
How do I formulate the WADL for it so that I get the #Context in the produced Java code?
2) I have got a update (PUT) method: update(#PathParam("isbn") String isbn, BookState st)
How do I formuate the WADL to get the BookState in the produced Java code?
Here is my current WADL, which does not do it:
<resource path="/{isbn}">
....
<method name="GET" id="get" >
<request />
<response>
<representation mediaType="application/xml" element="prefix1:book" />
</response>
</method>
<method name="PUT" id="update" >
<request>
<representation mediaType="application/xml" element="prefix1:book" />
</request>
<response>
<representation mediaType="application/octet-stream" />
</response>
</method>
</resource>