【问题标题】:POST using SpringTemplate between 2 different projects在 2 个不同项目之间使用 SpringTemplate POST
【发布时间】:2016-01-23 16:31:26
【问题描述】:

我正在开发一个管理教授预订的应用程序,它应该在使用 WebServices 的其他项目中使用不同的功能。 我有一个我在名为 WSSpring 的项目中制作的 RestController 和 这是它的代码。

package org.icda.entry;
import java.util.List;
import org.icda.entry.entities.Professor;
import org.icda.entry.professor.metier.IProfessorMetier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/Profs")
public class ProfessorController {

@Autowired
private IProfessorMetier professorMetier;

@RequestMapping(value = "/sortProfs", method = RequestMethod.GET)
public List<Professor> sortPorfessors() {
    return professorMetier.sortPorfessors();
}

@RequestMapping(value = "/allProfs", method = RequestMethod.GET)
public List<Professor> listAllProfessors() {
    return professorMetier.listAllProfessors();
}

@RequestMapping(value = "/findProf/{id}", method = RequestMethod.GET)
public Professor findOne(@PathVariable Long id) {
    return professorMetier.findOne(id);
}

 @RequestMapping(value = "/saveProf", method = RequestMethod.POST)
 public Professor save(@RequestBody Professor p) {
    return professorMetier.save(p);
 }
}

现在我必须让存在于另一个名为 WSSpringClient 的项目中的客户端应用程序与 RestController 进行通信。 我开发了一个基于 RestTemplate 的解决方案。 这就是我的客户端类的样子。

package org.icda.client.metier;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.client.RestTemplate;

public class ProfessorClient implements IProfessorClient {

public static final String REST_SERVICE_URI = "http://localhost:8080 /entry/Profs";

public static void main(String[] args) {
  ProfessorClient professor = new ProfessorClient();
  professor.listAllProfessors();
  System.out.println("Sorting .....");
  professor.sortPorfessors();
  Object object = professor.findOne(2L);
  System.out.println(object.toString());
}

@SuppressWarnings("unchecked")
@Override
public List<Object> listAllProfessors() {
    RestTemplate restTemplate = new RestTemplate();
    List<Object> objects = restTemplate.getForObject(REST_SERVICE_URI
            + "/allProfs", List.class);
    for (Object o : objects) {
        System.out.println(o.toString());
    }
    return objects;
}

@SuppressWarnings({ "unchecked" })
@Override
public List<Object> sortPorfessors() {
    RestTemplate restTemplate = new RestTemplate();
    List<Object> objects = restTemplate.getForObject(REST_SERVICE_URI
            + "/sortProfs", List.class);
    for (Object o : objects) {
        System.out.println(o.toString());
    }
    return objects;
}

@Override
public Object findOne(Long id) {
    Map<String, Long> profsMap = new HashMap<String, Long>();
    profsMap.put("id", id);
    RestTemplate restTemplate = new RestTemplate();
    Object object = restTemplate.getForObject(REST_SERVICE_URI
            + "/findProf/{id}", Object.class, profsMap);
    System.out.println("Searching for user number....."+id);
    return object;
}

@SuppressWarnings("unused")
@Override
public Object save(Object p) {
    RestTemplate restTemplate = new RestTemplate();
    URI uri = restTemplate.postForLocation(REST_SERVICE_URI + "/saveProf",p, Object.class);
    return p;
}
} 

我的问题是我不知道如何发布教授对象是 你可以在最后一个方法中看到 RestTemplate。

如果你有任何建议,谢谢分享。

【问题讨论】:

    标签: spring web-services rest post resttemplate


    【解决方案1】:

    我想您的/saveProf 端点可以接受ProfessorJSONXML 表示。因此,您将在您的POST 调用中发送Professor 的表示,例如JSON,然后save 处理程序方法将使用其注册的HttpMessageConverter 读取您的HTTP 消息正文并填充Professor 对象。

    假设你要发送这个JSON

    {
        "name": "someone",
        "degree": "PhD"
    }
    

    你应该在你的客户端写这样的东西:

    // Prepare professor representation
    Map<String, Object> professor = new HashMap<>();
    professor.put("name", "someone");
    professor.put("degree", "PhD");
    
    // Prepare request
    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.CONTENT_TYPE, "application/json");
    HttpEntity<Map<String, Object>> request = new HttpEntity<>(professor, headers);
    
    // Send prepared request
    ResponseEntity<String> response = template.postForEntity(REST_SERVICE_URI + "/saveProf", request, String.class);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-15
      • 2018-03-10
      相关资源
      最近更新 更多