使用Java代码来使用Restful Web服务

时间:2016-10-08 21:35:32

标签: java spring web-services rest jersey

我在JBoss上运行以下REST服务:

@POST
@Path("/postTest")
@Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON })
public Response PostTest(MyOwnUserData user)
{
    return Response.ok(user, MediaType.APPLICATION_JSON).build();
}

在另一个程序中,我试图使用以下代码调用此方法:

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.HttpEntity;
import java.util.Arrays;

public static void main(String[] args) 
{
    String mainUrl = "http://localhost:8080/services/users/test/posTest";

    ResponseEntity<MyOwnUserData > n2 = null;
    RestTemplate n = new RestTemplate();

    MyOwnUserData u = new MyOwnUserData ();

    HttpHeaders headers = new HttpHeaders();
    // This is needed to set the content-type header as application/json. The web service will throw a 415 Media Type unsupported if you dont set this.
    headers.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity<MyOwnUserData> entity = new HttpEntity(u,headers);

    n2 = n.postForEntity(mainUrl,u, MyOwnUserData .class);

}

当我执行第二个程序时,它总是给出BAD_REQUEST

错误:

Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type

JBOSS执行时记录错误:

23:44:49,256 ERROR [org.jboss.resteasy.resteasy_jaxrs.i18n] (http-localhost/127.0.0.1:8080-1) RESTEASY000100: Failed executing POST /test/posTest: org.jboss.resteasy.spi.UnsupportedMediaTypeException: RESTEASY001055: Cannot consume content type
    at org.jboss.resteasy.core.registry.Segment.match(Segment.java:118) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
    at org.jboss.resteasy.core.registry.SimpleSegment.matchSimple(SimpleSegment.java:34) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
    at org.jboss.resteasy.core.registry.RootSegment.matchChildren(RootSegment.java:328) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
    at org.jboss.resteasy.core.registry.SimpleSegment.matchSimple(SimpleSegment.java:45) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
    at org.jboss.resteasy.core.registry.RootSegment.matchChildren(RootSegment.java:328) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
    at org.jboss.resteasy.core.registry.SimpleSegment.matchSimple(SimpleSegment.java:45) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
    at org.jboss.resteasy.core.registry.RootSegment.matchChildren(RootSegment.java:328) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
    at org.jboss.resteasy.core.registry.SimpleSegment.matchSimple(SimpleSegment.java:45) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
    at org.jboss.resteasy.core.registry.RootSegment.matchChildren(RootSegment.java:328) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
    at org.jboss.resteasy.core.registry.RootSegment.matchRoot(RootSegment.java:375) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
    at org.jboss.resteasy.core.registry.RootSegment.matchRoot(RootSegment.java:368) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
    at org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(ResourceMethodRegistry.java:349) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousDispatcher.java:191) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:124) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-2.jar:1.0.2.Final-redhat-2]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
    at ar.com.flexibility.app.filter.ApiOriginFilter.doFilterInternal(ApiOriginFilter.java:46) [classes:]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:246) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:231) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.5.0.Final-redhat-21.jar:7.5.0.Final-redhat-21]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:150) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:854) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:926) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
    at java.lang.Thread.run(Unknown Source) [rt.jar:1.8.0_101]

3 个答案:

答案 0 :(得分:0)

尝试替换

ResponseEntity<MyOwnUserData> n2 = null;
n2 = n.postForEntity(mainUrl,u, MyOwnUserData.class);

file_content = File.read('path to file with extension');
MyModel.create(text: file_content);

答案 1 :(得分:0)

可以使用

 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/test/postTest");

 n.postForEntity(builder.build().encode().toUri(), u, MyOwnUserData.class)

答案 2 :(得分:0)

你需要做这样的事情:

// Set the message converter so as to enable marshalling/demarshalling for json
restTemplate.setMessageConverters(Arrays.asList(new MappingJackson2HttpMessageConverter()));

HttpHeaders headers = new HttpHeaders();
// This is needed to set the content-type header as application/json. The web service will throw a 415 Media Type unsupported if you dont set this.
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<MyOwnUserData> entity = new HttpEntity<MyOwnUserData>(u,headers);
Response response = restTemplate.postForObject(mainUrl, entity, Response.class);