简体   繁体   English

如何使用Java使用此Web服务?

[英]How can i consume this webservice with Java?

this is a very simple question apparently but i was unavaible to find a certain information searching in google. 这显然是一个非常简单的问题,但是我无法在Google中找到某些信息。 Let me tell you, i have a jboss running in my computer with only this webservice: 我告诉你,我的计算机上只有一个运行此jservice的jboss:

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.springframework.stereotype.Controller;

@Controller
@Path("/services/test")
public class TestController 
{
    @POST
    @Path("/reg")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces({ MediaType.APPLICATION_JSON })
    public Response checkMock(String paramx)
    {
        return Response.ok("Paramx: " + paramx,MediaType.APPLICATION_JSON).build();
    }

}

Ok, to test it i use "Advance REST Client" chrome plugin and it is very simple, look this picture: 好的,要测试它,我使用“ Advance REST Client” chrome插件,它非常简单,请看这张图片:

在此处输入图片说明

Well the question is, how can i do the same but with some java code in another program? 好了,问题是,除了另一个程序中的某些Java代码外,我该怎么做? What objecto do i require? 我需要什么反对?

Best regards! 最好的祝福!

Use Spring RestTemplate to consume a Rest service. 使用Spring RestTemplate来使用Rest服务。

You need to first register the RestTesmplate into spring context 您需要先将RestTesmplate注册到spring上下文中

<bean class="org.springframework.web.client.RestTemplate" id="restTemplate">
   <constructor-arg>               
        <bean class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory"/> 
    </constructor-arg>
 </bean> 

Once you registered your resttemplete in spring context you can inject that in your service class and call the exchange method 在spring上下文中注册resttemplet之后,您可以将其注入服务类并调用exchange方法

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<?> httpEntity = new HttpEntity<Object>(payLoad, headers);

ResponseEntity<ResponseObject> response = restTemplate.exchange(ENDPOINT, HttpMethod.POST, httpEntity, ResponseObject.class);
ResponseObject resObj =  response.getBody();

Here payLoad is your request object and ResponseObject 这里的payLoad是您的请求对象和ResponseObject

To unmarshall the response directly into to response object you need to register the message converters 要将响应直接解组到响应对象中,您需要注册消息转换器

List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(ResponseObject.class);
MarshallingHttpMessageConverter marshallingHttpMessageConverter = new MarshallingHttpMessageConverter(marshaller, marshaller);
marshallingHttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML));
messageConverters.add(marshallingHttpMessageConverter);
restTemplate.setMessageConverters(messageConverters);   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM