在Spring MVC中使用Restful api

时间:2018-03-28 09:32:09

标签: java rest web-services spring-mvc

我是Spring MVC的新手。我需要知道如何在UI中使用RESTful api。 而且我还需要知道我们可以将api用于同一个应用程序,或者我们将创建新的应用程序来使用REST生成的这些api。我在我的项目中构建了一个REST api,并在同一个项目中使用api和以下代码。但它没有用。

RestClient.java

package com.spring.template;

import org.springframework.web.client.RestTemplate;

import com.spring.model.Employee;

public class RestClient {

    public static void main(String[] args) {

        try {

            RestTemplate restTemplate = new RestTemplate();
            final String base_url = "http://localhost:8080/SpringWebSevices/";
            Employee employee = restTemplate.getForObject(base_url, Employee.class, 200);
            System.out.println("Id:"+employee.getEmpid());
        }
        catch (Exception e) {
            // TODO: handle exception
            System.out.println(e.getMessage());
        }

    }
}

EmployeeController.java

package com.spring.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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;

import com.spring.model.Employee;
import com.spring.service.EmployeeService;

@RestController
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @RequestMapping(value ="/", method = RequestMethod.GET, produces ="application/json") 
    public ResponseEntity<List<Employee>> employees() {
        HttpHeaders headers = new HttpHeaders();
        List<Employee> employee = employeeService.getEmployees();
        if(employee == null) {
            return new ResponseEntity<List<Employee>>(HttpStatus.NOT_FOUND);
        }
        headers.add("Number of records found:", String.valueOf(employee.size()));
        return new ResponseEntity<List<Employee>>(employee, HttpStatus.OK);
    }

    @RequestMapping(value="/employee/add", method = RequestMethod.POST , produces ="application/json")
    public ResponseEntity<Employee> addEmployee(@RequestBody Employee employee) {
        HttpHeaders headers = new HttpHeaders();
        if(employee == null) {
            return new ResponseEntity<Employee>(HttpStatus.BAD_REQUEST);
        }
        employeeService.createEmployee(employee);
        headers.add("Added employee id:", String.valueOf(employee.getEmpid()));
        return new ResponseEntity<Employee>(employee, headers, HttpStatus.CREATED);
    }

    @RequestMapping(value = "/employee/edit/{id}",method=RequestMethod.PUT)
    public ResponseEntity<Employee> editEmployee(@PathVariable("id") int empid,@RequestBody Employee employee) {
        HttpHeaders headers = new HttpHeaders();
        Employee isExist = employeeService.getEmployee(empid);
        if(isExist == null) {
            return new ResponseEntity<Employee>(HttpStatus.NOT_FOUND);
        } else if(employee == null) {
            return new ResponseEntity<Employee>(HttpStatus.BAD_GATEWAY);
        }
        employeeService.updateEmployee(employee);
        headers.add("Employee updated:", String.valueOf(employee.getEmpid()));
        return new ResponseEntity<Employee>(employee,headers,HttpStatus.OK);
    }

    @RequestMapping(value = "/employee/delete/{id}", method =RequestMethod.DELETE)
    public ResponseEntity<Employee> deleteEmployee(@PathVariable("id") int empid) {
        HttpHeaders headers = new HttpHeaders();
        Employee employee = employeeService.getEmployee(empid);
        if(employee == null) {
            return new ResponseEntity<Employee>(HttpStatus.NOT_FOUND);
        }
        employeeService.deleteEmployee(empid);
        headers.add("Employee deleted:", String.valueOf(empid));
        return new ResponseEntity<Employee>(employee, headers, HttpStatus.NO_CONTENT);
    }
}

这是我得到的错误:

nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.spring.model.Employee out of START_ARRAY token

2 个答案:

答案 0 :(得分:0)

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.spring.model.Employee out of START_ARRAY token

这看起来像是将Employee的列表发送到某个接受单个Employee作为参数的方法。可能性:

  • addEmployee
  • editEmployee

答案 1 :(得分:0)

当您尝试将结果分配给行中的单个Employee对象时,您的控制器正在返回List<Employee>

Employee employee = restTemplate.getForObject(base_url, Employee.class, 200);

您正面临类型不兼容,您可以尝试

ResponseEntity<? extends ArrayList<Employee>> responseEntity = restTemplate.getForEntity(base_url, (Class<? extends ArrayList<Employee>)ArrayList.class, 200);

我还没有测试过,但它应该有用。