The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

JohnnyMule (2) [Avatar] Offline
#1
I have not been able to successfully retrieve the JSON list from the Brew Service using REST and I am hoping one of you can spot my error.

All my attempts have only resulted in 404 responses.

Thanks in advance for your assistance!

========================================= First up is the configuration for the flow.

<context:property-placeholder location="/mule.properties"/>
<flow name="brewRESTService" doc:name="brewRESTService">
<http:inbound-endpoint >
exchange-pattern="request-response"
host="${host}"
port="${port}"
doc:name="HTTP"/>
<logger message="'HTTP request'#[message.inboundProperties]" level="INFO" doc:name="Logger"/>
<response>
<logger message="'HTTP Response'#[message.outboundProperties]" level="INFO" doc:name="Logger"/>
</response>
<jersey:resources doc:name="REST">
<component class="com.prancingdonkey.service.BrewServiceImpl"/>
</jersey:resources>
</flow>
</mule>

I am using a mule.properties file with the host and port defined.

host=localhost
port=8091

========================================= Next is the code starting with the Brew service:
package com.prancingdonkey.service;

import com.prancingdonkey.model.Brew;

import java.util.List;

public interface BrewService
{
List<Brew> getBrews();
}

========================================= Next the BrewServiceImpl:
package com.prancingdonkey.service;

import com.prancingdonkey.model.Brew;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;


@Path("/brews")
public class BrewServiceImpl implements BrewService
{
@GET
@Produces("application/json")
public List<Brew> getBrews() {
return Brew.findAll();
}
}

========================================= Finally the model:
package com.prancingdonkey.model;

import java.util.List;
import com.google.common.collect.ImmutableList;

public class Brew
{
String name;
String description;

public Brew(String name, String description)
{
this.name = name;
this.description = description;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public String getDescription()
{
return description;
}

public void setDescription(String description)
{
this.description = description;
}

static public List<Brew> findAll()
{
return ImmutableList.of(new Brew("Hobbit IPA", "Hobbit IPA"), new Brew("Frodos Lager", "Frodos Lager"));
}
}

========================================= Test Harness

I am using Chrome Postman to send the GET request to the service using the following call

http://localhost:8091/rest/brews/list

I also tried cURL with the same result.

=========================================
The loggers I setup show that communication between Chrome/Postman (or cURL) and the MuleSoft application container is working as expected. Just not wiring this up somehow to retrieve the JSON data

INFO 2014-10-05 08:50:35,880 [[msia_brewservice_jax].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: 'HTTP request'{http.request=/rest/brews/list, Keep-Alive=false, http.headers={Keep-Alive=false, Connection=false, User-Agent=curl/7.36.0, Host=localhost:8091, Accept=*/*}, http.query.string=, Accept=*/*, http.query.params={}, MULE_REMOTE_CLIENT_ADDRESS=/127.0.0.1:3138, Connection=false, User-Agent=curl/7.36.0, Host=localhost:8091, http.context.path=/, http.version=HTTP/1.1, http.method=GET, http.context.uri=http://localhost:8091, http.relative.path=rest/brews/list, http.request.path=/rest/brews/list, MULE_ORIGINATING_ENDPOINT=endpoint.http.localhost.8091}
INFO 2014-10-05 08:50:35,988 [[msia_brewservice_jax].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: 'HTTP Response'{http.status=404, MULE_ENCODING=UTF-8}
David Dossot (233) [Avatar] Offline
#2
Re: Chapter 3 REST example 404
> All my attempts have only resulted in 404 responses

Have you tried: http://localhost:8091/brews ?

Looking at your configuration above that's where it should be, because:

- the http:inbound-endpoint is bound to the / path
- the brews resource is bound to /brews

so the combined path should be /brews
JohnnyMule (2) [Avatar] Offline
#3
Re: Chapter 3 REST example 404
yes -- that worked! And thanks for the amazingly quick turn around. Wow.

Now this makes better sense, since I was trying to understand where /rest/brews/list URI were being defined. Thinking that I had made a mistake somewhere that was causing the 404.

Now I understand:

The config settings in the flow bind the REST service to the BrewServiceImpl

<jersey:resources doc:name="REST">
<component class="com.prancingdonkey.service.BrewServiceImpl"/>
</jersey:resources>

And the @Path("/brews") annotation in the BrewServiceImpl specifies the path to the resource beyond http://localhost:8091

Playing with this to better understand, I made this change @Path("/rest/brews") and then re-ran the request with http://localhost:8091/rest/brews and it worked.

I know this is all very obvious, and I appreciate your time helping me!
David Dossot (233) [Avatar] Offline
#4
Re: Chapter 3 REST example 404
Glad I could help smilie