Need help with client call a rest service

264 weergaven
Naar het eerste ongelezen bericht

Stephan Gerth

ongelezen,
2 mrt 2015, 10:43:4302-03-2015
aan dropwiz...@googlegroups.com
Hi all,

i try to migrate a running hibernate backend to a dropwizard server and stuck with the client call. 

The resource method:

@Path(value = "/ox_country")
public class OxCountryResource {
. . .

    @POST
    @Timed
    @Path("/paged-ro")
    @UnitOfWork
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public OxResultObject getAllCountriesPagedAsResulObject(OxCountryLookup _lookup, @QueryParam("start") long _start,
            @QueryParam("size") long _size) {
        if (_lookup == null) {
            System.out.println("lookup bean is null");
        }

        long start = _start;
        long size = _size;
        return oxCountryService.getPagedCountrys(_lookup, start, size);
    }


my client call:

OxResultObject<?> ro = null;
OxCountryLookup lookup = new OxCountryLookup();
lookup.setCode2("E");
int start = 0;
int size = 10;

try {
WebTarget webTarget = client.target("http://192.168.100.41:8088");
WebTarget webTargetResource = webTarget.path("ox_country/paged-ro");
WebTarget webTargetResourceWithParam = webTargetResource.queryParam("start", start).queryParam("size", size);

Invocation.Builder invocationBuilder = webTargetResourceWithParam.request(MediaType.APPLICATION_JSON);
invocationBuilder.header("some-header", "true");
response = invocationBuilder.post(Entity.entity(lookup, MediaType.APPLICATION_JSON));

if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}

// How do get the remote IP ????
System.out.println(response.getStatus() + " " + response.getStatusInfo() + "  " + response.getMetadata().getFirst("Date") + " "
       + "Content-Length: " + response.getMetadata().getFirst("Content-Length"));

} catch (Exception e) {
response.close();
e.printStackTrace();

} finally {
response.close();
}

The OxCountryLookup is a helper bean which holds search values which are evaluated in the hibernate service method.
The OxResultObject is a helper class which holds the result list with the paged objects and a total count of possible records.

My problem is now i get not the 'OxCountryLookup' object to the rest service. I get all times a http 400 which pointed to a false header?

Any help are welcome.

thanks
Stephan

Stephan Gerth

ongelezen,
3 mrt 2015, 03:42:5703-03-2015
aan dropwiz...@googlegroups.com
Get it to run by using the @BeanParam annotation. 

myMethod(@BeanParam OxCountryLookup _lookup, ...){}


I'm now really confused why most of the samples are NOT using the @BeanParam annotation. 
Any explanations are  very welcome.

thanks
Stephan

Camille Fournier

ongelezen,
3 mrt 2015, 13:12:1603-03-2015
aan dropwiz...@googlegroups.com
Can you point to a sample that you would expect to be using a BeanParam but is not?

Thanks,
C

--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stephan Gerth

ongelezen,
4 mrt 2015, 05:15:0704-03-2015
aan dropwiz...@googlegroups.com

@POST public User addUser(User user) { User createdUser = userDao.save(user); return createdUser; }
or:
 @POST
    public Todo add(@Valid Todo todo){
        todos.add(todo);
        return todo;
    }
In fact i have problems to call a rest resource for @POST. 
I get an object back from the rest @GET method. I change one property and try to save it with the @POST method back.
But the values are all times NULL. 

Michael Kidd

ongelezen,
4 mrt 2015, 12:05:2804-03-2015
aan dropwiz...@googlegroups.com
You shouldn't need the @BeanParam annotation...

is the server side generating any logging info that might provide insight?
Can you add any logging to confirm that the client is doing what you'd expect, in order to determine if the issue is client or server side?

Stephan Gerth

ongelezen,
4 mrt 2015, 12:17:5904-03-2015
aan dropwiz...@googlegroups.com
I'm near to jump from the house :-(

I have the eclipse TCP monitor running. My client is sending this:

. . .
OxCountryLookup lookup = new OxCountryLookup();
lookup.setCode2("E");
int start = 0;
int size = 10;
. . .
try {
WebTarget webTarget = client.target(restServer.getRestServerAddress());
WebTarget webTargetResource = webTarget.path("ox_country/paged-ro");
WebTarget webTargetResourceWithParam = webTargetResource.queryParam("start", start).queryParam("size", size);

Invocation.Builder invocationBuilder = webTargetResourceWithParam.request(MediaType.APPLICATION_JSON);
String json = mapper.writeValueAsString(lookup);
System.out.println(json);

response = invocationBuilder.post(Entity.entity(lookup, MediaType.APPLICATION_JSON));

TCP monitor shows this:
{"code2":"E","nameDe":null,"nameEn":null,"empty":false}

-------------------------------------------

On the serverside in my @POST method:

   public OxResultObject getAllCountriesPagedAsResulObject(@Valid OxCountryLookup _lookup,
            @QueryParam("start") long _start, @QueryParam("size") long _size) {
  . . .
If i debug this, all times the '_lookup' object have null values. Only the @QueryParam for the paging is filled correctly.

I really don't not know what i'm doing wrong.

thanks for all suggestions
Stephan

Stephan Gerth

ongelezen,
5 mrt 2015, 11:14:0205-03-2015
aan dropwiz...@googlegroups.com
Have it.  Wow that was heavy.  Don't know if there is a global configuration for that.
At last the @POST method code is never reached. All times a 400 http error.

In my lookup helper bean with only 3 fields with its setter/getter is an additionally  isEmpty() method that i need
for my hibernate stuff. By annotating  this method with @JsonIgnore the resource method is running well.

Is there a special configuration so that jersey doesn't react on methods that have no fields? like hashCode()

thanks all
Stephan
Bericht is verwijderd

Michael Kidd

ongelezen,
5 mrt 2015, 11:53:2905-03-2015
aan dropwiz...@googlegroups.com
You may be looking for something like this in your bootstrap:

bootstrap.getObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

alexander anguiano

ongelezen,
5 mrt 2015, 15:41:4005-03-2015
aan dropwiz...@googlegroups.com
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.sun.jersey.api.client.Client;

for the client side use this for 0.70

JacksonJsonProvider jacksonJsonProvider = 
   new JacksonJaxbJsonProvider()
       .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
config.getSingletons().add(jacksonJsonProvider);
Client client = Client.create(config);

Stephan Gerth

ongelezen,
9 mrt 2015, 12:19:0409-03-2015
aan dropwiz...@googlegroups.com
Hi all, many many thanks for all of you. The recommandations helps me a lot.



I have now a questions about the 'best' pattern for getting the response object back from the resource.
I have seen a lot of different handlings ('personally') of the Response back to the caller.

So i'm interesting what is the better / cleanest way to response the object:

1. wrap the object in the Response object:
public Response updateBook(Book book) {
  // some code here

return Response.ok(book).build();
}
2. return direct the object: public Book updateBook(Book book){ // some code here return book; } thanks Stephan

Danil Suits

ongelezen,
10 mrt 2015, 11:38:5410-03-2015
aan dropwiz...@googlegroups.com
I like this question.

I've found myself using Response objects so that I can control the response status; for instance

// 202 Accepted
return Response.accepted(report).build();

Stephan Gerth

ongelezen,
13 mrt 2015, 08:47:2813-03-2015
aan dropwiz...@googlegroups.com
Yes, i use now all times the Response object for the return. 

Thanks all.


On Monday, March 2, 2015 at 4:43:43 PM UTC+1, Stephan Gerth wrote:
Allen beantwoorden
Auteur beantwoorden
Doorsturen
0 nieuwe berichten