Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rest-http POST method #108

Open
ghost opened this issue May 26, 2014 · 10 comments
Open

rest-http POST method #108

ghost opened this issue May 26, 2014 · 10 comments

Comments

@ghost
Copy link

ghost commented May 26, 2014

i want to sent json by post method
after change integration configuration
and write test i send object but response is failed

<int-http:inbound-gateway id="inboundEmployeeSearchRequestGateway" 
        supported-methods="POST"
        request-payload-type="org.springframework.integration.samples.rest.domain.Employee"
        request-channel="employeeSearchRequest"
        reply-channel="employeeSearchResponse"
        mapped-response-headers="Return-Status, Return-Status-Msg, HTTP_RESPONSE_HEADERS"
        path="/services/employee/{id}/search"
        reply-timeout="50000"  
        >
@Test
    public void testGetEmployeeAsJsonPOST() throws Exception {
        Map<String, Object> employeeSearchMap = getEmployeeSearchMap("0");

        final String fullUrl = "http://localhost:8080/rest-http/services/employee/{id}/search?format=json";
        HttpHeaders headers = getHttpHeadersWithUserCredentials(new HttpHeaders());
        headers.add("Accept", "application/json");
        Employee em=new Employee(6, "ali akbar", "azizkhani");
        HttpEntity<Employee> request = new HttpEntity<Employee>(em,headers);
        ResponseEntity<?> httpResponse = restTemplate.exchange(fullUrl, HttpMethod.POST, request,EmployeeList.class, employeeSearchMap);
        logger.info("Return Status :" + httpResponse.getHeaders().get("X-Return-Status"));
        logger.info("Return Status Message :" + httpResponse.getHeaders().get("X-Return-Status-Msg"));
        assertTrue(httpResponse.getStatusCode().equals(HttpStatus.OK));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        jaxbJacksonObjectMapper.writeValue(out, httpResponse.getBody());
        logger.info(new String(out.toByteArray()));
    }
@artembilan
Copy link
Member

Your changes works for me. Share, please, the StackTrace

@ghost
Copy link
Author

ghost commented May 26, 2014

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unexpected end-of-input within/between OBJECT entries
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@2392d604; line: 1, column: 101] (through reference chain: org.springframework.integration.samples.rest.domain.EmployeeList["employee"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected end-of-input within/between OBJECT entries
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@2392d604; line: 1, column: 101] (through reference chain: org.springframework.integration.samples.rest.domain.EmployeeList["employee"])
    at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:228)
    at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.read(MappingJackson2HttpMessageConverter.java:220)
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:95)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:784)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:769)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:549)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:510)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:453)
    at org.springframework.integration.samples.rest.RestHttpClientTest.testGetEmployeeAsJsonPOST(RestHttpClientTest.java:109)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Unexpected end-of-input within/between OBJECT entries
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@2392d604; line: 1, column: 101] (through reference chain: org.springframework.integration.samples.rest.domain.EmployeeList["employee"])
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:232)
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:197)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.wrapAndThrow(BeanDeserializerBase.java:1420)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:244)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2993)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2158)
    at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:225)
    ... 37 more
Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected end-of-input within/between OBJECT entries
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@2392d604; line: 1, column: 101]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1524)
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._skipWS(UTF8StreamJsonParser.java:2546)
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:708)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:235)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:227)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:204)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:525)
    at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:106)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242)
    ... 41 more

@artembilan
Copy link
Member

Looks like you changed something else, e.g. in the EmployeeSearchService

@ghost
Copy link
Author

ghost commented May 27, 2014

can you add some unit test like this and commit it thanks;

@Secured("ROLE_REST_HTTP_USER")
    public Message<EmployeeList> getEmployee(Message<?> inMessage){

        EmployeeList employeeList = new EmployeeList();
        Map<String, Object> responseHeaderMap = new HashMap<String, Object>();


        try{
            Employee expl=(Employee)inMessage.getPayload();
            employeeList.getEmployee().add(expl);
            MessageHeaders headers = inMessage.getHeaders();
            String id = (String)headers.get("employeeId");
            boolean isFound;
            if (id.equals("1")){
                employeeList.getEmployee().add(new Employee(1, "John", "Doe"));
                isFound = true;
            }else if (id.equals("2")){
                employeeList.getEmployee().add(new Employee(2, "Jane", "Doe"));
                isFound = true;
            }else if (id.equals("0")){
                employeeList.getEmployee().add(new Employee(1, "John", "Doe"));
                employeeList.getEmployee().add(new Employee(2, "Jane", "Doe"));             
                isFound = true;
            }else{              
                isFound = false;
            }           
            if (isFound){
                setReturnStatusAndMessage("0", "Success", employeeList, responseHeaderMap);
            }else{
                setReturnStatusAndMessage("2", "Employee Not Found", employeeList, responseHeaderMap);                              
            }

        }catch (Throwable e){

            setReturnStatusAndMessage("1", "System Error", employeeList, responseHeaderMap);
            logger.error("System error occured :"+e);
        }
        Message<EmployeeList> message = new GenericMessage<EmployeeList>(employeeList, responseHeaderMap);
        return message;     
    }

@artembilan
Copy link
Member

Do you mean this:

 Employee expl=(Employee)inMessage.getPayload();
 employeeList.getEmployee().add(expl);

?
What is your point? Just to check that we can transfer request object to response?
Although I don't see concern to add it to the sample, but there is need to understand the goal.

Thanks

@ghost
Copy link
Author

ghost commented May 27, 2014

i want send object (json) by post method and add to list and get list (json);
when i test it .both in unit test and using rest-client plugin response in failed and empty .
when i trace see that object have in payload but return message failed.
thanks

@artembilan
Copy link
Member

Strange: it works for me again:

09:50:11.714 INFO [main][org.springframework.integration.samples.rest.RestHttpClientTest] Return Status :[0]
09:50:11.715 INFO [main][org.springframework.integration.samples.rest.RestHttpClientTest] Return Status Message :[Success]
09:50:11.735 INFO [main][org.springframework.integration.samples.rest.RestHttpClientTest] {"employee":[{"employeeId":1,"fname":"ali","lname":"azizkhani"},{"employeeId":1,"fname":"John","lname":"Doe"},{"employeeId":2,"fname":"Jane","lname":"Doe"}],"returnStatus":"0","returnStatusMsg":"Success"}

Or you are using some old SAMPLES version, or I am wrong that test it against my Gradle branch.

I think we will add your tweaks to this sample, but already after we commit Gradle migration.

What might cause the issue, that I use Gradle and its Jetty pluging, but Maven sample uses Tomcat pluging.

As the conclusion: all your modifications are correct and have the right to life

@ghost
Copy link
Author

ghost commented May 27, 2014

i am using tomcat & maven

@ghost
Copy link
Author

ghost commented Jun 4, 2014

i think this is cause when i remove xml in negotiation and remove marshaler ;
i want to get json just why when i remove it this happen

@artembilan
Copy link
Member

Well, does it say that you haven't shown entire changes to the sample?
Can you do it now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant