如何使用 Restful Web 服务,使用 jersey.Iam 本主题的新内容

How to work with Restful web service using jersey.Iam new to this Topic

请建议 spring restful 网络服务..我有一个疑问,如果我在一个动态网络项目中有网络服务,我如何才能在客户端(或)获得价值 例如: 客户端(动态 web 项目)我给出了两个数据 2 和 3 如何将此数据发送到服务器端(另一个动态 web 项目)用于附加目的是 possible.how 服务器从另一个 [=15] 中的客户端获取数据=]一些代码。

您可以在单个 eclipse 工作区中拥有任意数量的项目,分别工作,比如 WSClient(WS 客户端代码)和 WSServer(使用它开发和部署的 WS)两个独立的项目。

这是使用 Spring MVC 开发 RESTful 服务的示例 Spring控制器:

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SpringRestController {
    @RequestMapping(method = { RequestMethod.GET }, value = { "/testApplication" }, consumes = MediaType.ALL_VALUE, produces = MediaType.ALL_VALUE)
    public String getServerInfo() {
        System.out.println("I got hit");
        String message = "Hit the end point";
        return message;
    }
}

部署上述服务后,您甚至可以使用浏览器使用它。一旦完成,然后使用 Service Client 来使用它。

您可以使用许多框架来使用 REST 网络服务,我使用的其中之一是 Jersey,它是基于 JAX 实现的-RS规格.

以下是 Jersey 客户端使用 REST Web 服务的示例代码

REST WS 项目

//Person pojo
@JsonInclude(Include.NON_NULL)      
public class Person implements Serializable  {

   private static final long serialVersionUID = 8979562848777300129L;

   private String firstName;
   private String lastName;
   private String email

   //setter and getter methods  
}

//REST WS controller
@RestController
@RequestMapping("/user")
public class SBUserController{

@RequestMapping(value = "/profile", method = RequestMethod.GET, consumes = "application/json", produces = "application/json") 
public Person fetchProfile() {

//implement service layer and dao layer to fetch user profile from DB
//testing purpose i am creating one person and returning back
    Person person  = new Person();
    person.setFirstName("John");
    person.setLastName("Terry");
    person.setEmail("john@gmail.com");

    return person;
}
}

//客户端

   Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8080/user/profile");

ClientResponse clientResponse = webResource
                                .accept(MediaType.APPLICATION_JSON)
                                .type(MediaType.APPLICATION_JSON)
                                .get(ClientResponse.class);

        if (clientResponse.getStatus() == 200) {
        //You need to have the same Person bean class in client project also
          Person person = clientResponse.getEntity(Person.class));
        }