将对象从Spring MVC 3 REST客户端发送到REST提供程序

Alv*_*vin 2 rest spring json spring-mvc

我的目的是创建一个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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

@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";
    }
}
Run Code Online (Sandbox Code Playgroud)

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

每次我调试这个,当我到达提供者时,我得到null.

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

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

Aks*_*hay 5

您的问题的原因:

  1. 如果要通过控制器接受数据@RequestParam,请求应包含数据,以及请求参数.想想包含的网址?empCode=xyz&password=abc.或者在请求体中作为empCode=xyz&password=abc

  2. Employee在客户端代码中发送对象的方式,该对象将被序列化到请求正文中.使用您提供的代码,客户端将发出请求,将employee对象转换为请求正文中的JSON表示.你的控制器方法无法读取的东西.(假设你在类路径中有映射jackson jar)

可能的解决方案:

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

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

希望这可以帮助.