繁体   English   中英

在Spring MVC中使用Restful API

[英]Consuming Restful api in Spring MVC

我是Spring MVC的新手。 我需要知道如何在UI中使用RESTful API。 而且我还需要知道我们可以将api用于同一个应用程序,还是我们将创建新的应用程序来使用REST生成的这些api。 我在项目中构建了一个REST 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

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

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

  • addEmployee
  • editEmployee

当您尝试将结果分配给该行中的单个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);

我还没有测试过,但是应该可以。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM