java-POST方法415错误

时间:2017-04-11 19:48:01

标签: java spring hibernate backbone.js

我有java EE项目。我在这个项目中使用Spring Hibernate用于服务器端和Backbone,RequireJS,Jquery用于客户端。我正在尝试使用POST方法保存但是HTTP 415错误返回。

我的Get方法运行正常。我在Postman上测试了它。

我的网址:localhost:8080 / rest / travel

EDITED

我的资源类

    @Component
@Path("/travel")
public class TravelResource {

        @Autowired
        private TravelService travelService;
       @Autowired
      private UserService userService;

    @POST
    @Produces(MediaType.APPLICATION_JSON+ ";charset=UTF-8")
    @Consumes(MediaType.APPLICATION_JSON)
        public TravelDTO save(TravelDTO dto) {
        dto.setUser(userService.get(dto.getUser().getUserID()));
          dto.setTravelStart(new Date());
          dto.setTravelEnd(new Date());

          return travelService.save(dto);
        }

    @PUT
    @Path("/{travelID}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
        public TravelDTO edit(@PathParam("travelID") int id, TravelDTO dto) {
        dto.setUser(userService.get(dto.getUser().getUserID()));
        dto.setTravelStart(new Date());
        dto.setTravelEnd(new Date());

        return travelService.edit(id,dto);
        }

    @DELETE
    @Path("/{travelID}")
    @Produces(MediaType.TEXT_PLAIN)
        public int delete(@PathParam("travelID") int id) {
        travelService.delete(id);
        return id;
        }
    @GET
    @Path("/{travelID}")
    @Produces(MediaType.APPLICATION_JSON)
        public TravelDTO get(int id) {
        return travelService.get(id);
        }
    @GET
    @Produces(MediaType.APPLICATION_JSON)
        public List<TravelDTO> getAll() {
        return travelService.getAll();

        }

    @GET
    @Path("/search/{travelStart}/{travelEnd}/" )
    @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
    public List<TravelDTO> search(@PathParam("travelStart") Date travelStart,
                                  @PathParam("travelEnd") Date travelEnd )
    {
        return travelService.search(travelStart,travelEnd);
    }

}

我的DTO课程

public class TravelDTO implements Serializable {

    public TravelDTO convert(Travel model) {
        this.setTravelID(model.getTravelID());
        this.setTravelCost(model.getTravelCost());
        this.setTravelStart(model.getTravelStart());
        this.setTravelEnd(model.getTravelEnd());
        this.setLocation(model.getLocation());
        this.setTravelPurpose(model.getTravelPurpose());
       this.setProjectCode(model.getProjectCode());
       this.setUser(new UserDTO().convert(model.getUser()));
        return this;
    }
        private int travelID;

       private String projectCode;

        private Date travelStart;

        private Date travelEnd;

        private String Location;

        private float travelCost;

        private UserDTO user;

        private String travelPurpose;

    public UserDTO getUser() {
        return user;
    }

    public void setUser(UserDTO user) {
        this.user = user;
    }

    public String getProjectCode() {
        return projectCode;
    }

    public void setProjectCode(String projectCode) {
        this.projectCode = projectCode;
    }

    public int getTravelID() {
            return travelID;
        }

        public void setTravelID(int travelID) {
            this.travelID = travelID;
        }

        public Date getTravelStart() {
            return travelStart;
        }

        public void setTravelStart(Date travelStart) {
            this.travelStart = travelStart;
        }

        public Date getTravelEnd() {
            return travelEnd;
        }

        public void setTravelEnd(Date travelEnd) {
            this.travelEnd = travelEnd;
        }

        public String getLocation() {
            return Location;
        }

        public void setLocation(String location) {
            Location = location;
        }

        public float getTravelCost() {
            return travelCost;
        }

        public void setTravelCost(float travelCost) {
            this.travelCost = travelCost;
        }

        public String getTravelPurpose() {
            return travelPurpose;
        }

        public void setTravelPurpose(String travelPurpose) {
            this.travelPurpose = travelPurpose;
        }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        TravelDTO travelDTO = (TravelDTO) o;

        return travelID == travelDTO.travelID;
    }

    @Override
    public int hashCode() {
        return travelID;
    }
}

我的服务类

@Service
public class TravelService {
        @Autowired
        private TravelDAO travelDAO;

        @Transactional
        @ResponseBody
        public TravelDTO save(TravelDTO dto) {
            Travel station = new Travel();
            station.convert(dto);
            try {
                station = travelDAO.persist(station);
            } catch (Exception e) {
                return null;
            }
            return dto.convert(station);
        }

        @Transactional
        @ResponseBody
        public TravelDTO edit(int id, TravelDTO dto) {
            Travel travel = new Travel();
            try {
                travel.convert(dto);
                travelDAO.merge(travel);
            } catch (Exception e) {
                return null;
            }
            return dto.convert(travel);
        }

        @Transactional
        @ResponseBody
        public boolean delete(int id) {
            try {
                Travel travel = travelDAO.find(id);
                travelDAO.remove(travel);
            } catch (Exception e) {
                return false;
            }
            return true;
        }

        public TravelDTO get(int id) {
            Travel travel;
            try {
                travel = travelDAO.find(id);
            } catch (Exception e) {
                return null;
            }
            return new TravelDTO().convert(travel);
        }

        public List<TravelDTO> getAll() {
            List<TravelDTO> travelDTOList = new ArrayList<TravelDTO>();
            try {
                for (Travel travel : travelDAO.findAll())
                    travelDTOList.add(new TravelDTO().convert(travel));
            } catch (Exception e) {
                return null;
            }
            return travelDTOList;
        }

        public List<TravelDTO> search(Date startTime, Date endTime)
                                      {
            List<TravelDTO> travelDTOList = new ArrayList<TravelDTO>();
            try {
                for (Travel station : travelDAO.search(startTime, endTime))
                    travelDTOList.add(new TravelDTO().convert(station));
            } catch (Exception e) {
                return null;
            }
            return travelDTOList;
        }




}

我的数据:

model:{"Location":"s","projectCode":"51","travelPurpose":"s","travelStart":"1.1.1","travelEnd":"8.12.2016"}

网络标签

enter image description here

5 个答案:

答案 0 :(得分:1)

HTTP 415来的时候

The origin server is refusing to service the request because the payload is in 
a format not supported by this method on the target resource.

参考:https://httpstatuses.com/415

您需要正确设置请求标头。根据您附加的图片,您正在传递Content-Type: application/x-www-form-urlencoded。 但是您的代码需要application/json

答案 1 :(得分:1)

我的错误原因是这个骨干代码:

Backbone.emulateJSON = true;

如果Backbone.emulateJSON=trueContent-Type= application/x-www-form-urlencoded

现在Backbone.emulateJSON=false; 问题已解决!

答案 2 :(得分:1)

如果您尝试在POSTMAN中进行呼叫,请添加Content-Type:application / json作为标头参数。

答案 3 :(得分:0)

    @POST
    @Produces(MediaType.APPLICATION_JSON+ ";charset=UTF-8")
    @Consumes(MediaType.APPLICATION_JSON)
        public TravelDTO save(TravelDTO dto) {
        dto.setUser(userService.get(dto.getUser().getUserID()));
          dto.setTravelStart(new Date());
          dto.setTravelEnd(new Date());

          return travelService.save(dto);
        }

你回来了:return travelService.save(dto);

您声明您正在返回HTTP JSON

然后只需向您的控制器添加注释@RestController,而不是@Controller

或只是向您的方法添加@ResponseBody注释。

答案 4 :(得分:0)

请将以下内容添加到您的maven中:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson2.version}</version>
    </dependency>