gpt4 book ai didi

json - 将对象从 Spring MVC 3 REST 客户端发送到 REST 提供者

转载 作者:行者123 更新时间:2023-12-02 21:54:29 26 4
gpt4 key购买 nike

我的目的是创建一个 Spring 3 MVC Web 客户端和 Spring MVC REST 提供程序。

这是我的 REST 提供程序的代码:

@RequestMapping("employee")
public class EmployeeController {
@Autowired
EmployeeDAO empDao;

@RequestMapping(value = "authenticateUser", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public String authenticateUser(HttpServletRequest request, @RequestParam Employee employee) {
String username = employee.getEmpCode();
String password = employee.getPassword();

String notExisting = "notExisting";
String successLogin = "successLogin";
String wrongPassword = "wrongPassword";

Employee retrievedEmployee = empDao.getById(username);
if(retrievedEmployee == null) {
return notExisting;
} else {
if(retrievedEmployee.getPassword().equals(password)) {
return successLogin;
} else {
return wrongPassword;
}
}
}
}

这里是我的网络客户端的代码:

@Controller
public class UserAuthenticationController {
private final static String SERVICEURL = "http://localhost:8080/cimweb";

/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/userAuthentication", method = RequestMethod.POST)
public @ResponseBody String userAuthentication(Locale locale, Model model, HttpServletRequest request) {
Employee employee = new Employee();
String username = request.getParameter("username");
String password = request.getParameter("password");

employee.setEmpCode(username);
employee.setPassword(password);

// Prepare acceptable media type
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.APPLICATION_JSON);

HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);
HttpEntity<Employee> entity = new HttpEntity<Employee>(employee, headers);

RestTemplate restTemplate = new RestTemplate();
String url = SERVICEURL + "/employee/authenticateUser";
try {
ResponseEntity<Employee> result = restTemplate.exchange(url, HttpMethod.POST, entity, Employee.class);
} catch(Exception e) {
e.printStackTrace();
}

return "home";
}
}

我只想将对象 Employee 传递给 REST 提供程序,以便我可以处理它并将字符串从 REST 提供程序返回给客户端。

每次我调试此问题时,当我到达提供程序时,我都会得到 null。

我知道我错过了很多东西,并且我已经阅读了一些内容,但大多数情况下我看到的是来自提供者的直接请求,而不是对象。

此外,我是否应该将任何必需的 bean 放入 servlet-context.xml 中?

最佳答案

您遇到问题的原因:

  1. 如果您想通过 @RequestParam 接受 Controller 中的数据,则请求应包含数据以及请求参数。认为 url 包含 ?empCode=xyz&password=abc。或者在请求正文中为 empCode=xyz&password=abc

  2. 您在客户端代码中发送 Employee 对象的方式,该对象将被序列化到请求正文中。使用您提供的代码,客户端将发出请求,并将员工对象转换为请求正文中的 JSON 表示形式。您的 Controller 方法无法读取某些内容。 (假设您在类路径中有映射 Jackson jar)

可能的解决方案:

  1. 更改 Controller 方法签名,使用@RequestBody而不是@RequestParam,它告诉Spring提取Employee的值请求正文中的 code> 对象。它将检测到这是一个 json 请求,它将在上下文中查找 JSON Http 消息转换器并创建您的员工对象。

  2. 第二个选项是保留 Controller 签名,但更改发出请求的方式。在客户端中,实例化 RestTemplate 实例后,将 Form Http Message 转换器设置到其转换器属性中。这将告诉客户端像表单提交一样 POST 数据。您的 @RequestParam 带注释的 Controller 方法参数可以读取它。

希望这有帮助。

关于json - 将对象从 Spring MVC 3 REST 客户端发送到 REST 提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18012546/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com