2

我的意图是创建一个 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 提供者返回给客户端。

每次我调试这个时,当我到达提供者时,我都会得到空值。

我知道我错过了很多东西,我已经阅读了一些,但我看到的大部分都是来自提供者的直接请求,而不是对象。

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

4

1 回答 1

5

您的问题的原因:

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

  2. 您在客户端代码中发送Employee对象的方式,对象将被序列化到请求正文中。使用您提供的代码,客户端将发出请求,其中员工对象在请求正文中转换为其 JSON 表示形式。您的控制器方法无法读取的内容。(假设您在类路径中有映射杰克逊罐子)

可能的解决方案:

  1. 更改控制器方法签名,而不是@RequestParam使用@RequestBody,它告诉 SpringEmployee从请求正文中提取对象的值。它会检测到这是一个 json 请求,它会在上下文中查找 JSON Http Message Converter 并创建您的员工对象。

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

希望这可以帮助。

于 2013-08-02T15:39:35.850 回答