2014-08-27 2 views
1

POST 메서드를 수행하려고하는 JS 파일이 있지만 입력으로 특정 Java 객체가 필요합니다. 여기 각도 JS에서 Java 객체를 보내는 방법

서버에서 찾는 방법의 서명이다

여기
@Path("/branches") 
public class BranchResource { 

@POST 
@Produces(MediaType.APPLICATION_JSON) 
@Consumes(MediaType.APPLICATION_JSON) 
public Response post(BranchDescriptor branch) { 
. 
. 

인 I는 다음의 에러 수신 된

$scope.retry = function() { 
    $http({ 
      url : "rest/branches", 
      method : "POST", 
      dataType : "json",//not sure is needed 
      data : "way to get Branch descriptor " 
      headers : { 
       "Content-Type" : "application/json; charset=utf-8", 
       "Accept" : "application/json" 
      } 
    }).success(function(data) { 
      $scope.items = data; 
    }).error(function(data) { 
      alert('err'); 
    }); 
}; 

(각도 사용) JS 기능 :

??? 27, 2014 3:27:48 PM com.sun.jersey.spi.container.ContainerRequest getEntity 
SEVERE: A message body reader for Java class xxx.api.BranchDescriptor, and Java type class xxx.BranchDescriptor, and MIME media type application/octet-stream was not found. 
The registered message body readers compatible with the MIME media type are: 
application/octet-stream -> 
    com.sun.jersey.core.impl.provider.entity.ByteArrayProvider 
    com.sun.jersey.core.impl.provider.entity.FileProvider 
    com.sun.jersey.core.impl.provider.entity.InputStreamProvider 
    com.sun.jersey.core.impl.provider.entity.DataSourceProvider 
    com.sun.jersey.core.impl.provider.entity.RenderedImageProvider 
*/* -> 

이렇게 데이터를 추가하려고했습니다.

data : { "_protectedBranch" : "pf_something", "_newBranch" : "some_branch", "_commitId" : "some_commit", "_commiter" : "someone" },

다음과 같은 오류 있어요 :

??? 27, 2014 3:42:46 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException 
SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container 
org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "_protectedBranch" (Class xxx.BranchDescriptor), not marked as ignorable 
at [Source: [email protected]; line: 1, column: 22] (through reference chain: xxx.BranchDescriptor["_protectedBranch"]) 
    at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53) 

가 어떻게 전송을위한 BranchDescriptor 개체를 전달할 수 있습니까? 내가 무엇이 누락 되었습니까?

+0

당신이하려는 것은별로 의미가 없습니다. 자바 객체를 (적어도 클라이언트 측에서) JS로 생성 할 수 없다. Java와 JavaScript에는 공통점이 없습니다. REST 서비스를 사용하고 Java 기능에 필요한 특성을 전달한 다음 Java 서버 코드 내에서 오브젝트를 작성하는 것입니다. – dirkk

+0

또한 콘솔에서 오류를 공유 할 수 있습니다. – Jai

+0

질문은 어떻게 든 오도하는 것입니다. 재건하려고 노력하십시오. –

답변

1

OK, 내가 없어진 것, 그것은 분기를 생성하고 HTTP의 데이터에 보내 단지 방법이었다 발견 :

var branch = { 
      protectedBranch:"some_branch", 
      newBranch:"some_branch", 
      commitId: "some_commit", 
      commiter:"some_commiter" 
    } 
    $scope.retry = function() { 
     $http({ 
       url : "rest/branches", 
       method : "POST", 
       data : branch, 
       headers : { 
        "Content-Type" : "application/json; charset=utf-8", 
        "Accept" : "application/json" 
       } 
     }).success(function(data) { 
       $scope.items = data; 
     }).error(function(data) { 
       alert('err'); 
     }); 

다른 사람들에게 도움이되기를 바랍니다.

0

당신은 dataType를 필요로하지 않으며, 당신은 같은 문자열 대신 객체를 전송 시도 할 수 있습니다 : 워드 프로세서에서

$http({ 
    url : "rest/branches", 
    method : "POST", 
    data : {branch : "way to get Branch descriptor" }, 
    headers : { 
     "Content-Type" : "application/json; charset=utf-8", 
     "Accept" : "application/json" 
    } 
}) 

:

The $http service will automatically add certain HTTP headers to all requests.

  • Accept: application/json, text/plain, */*
  • Content-Type: application/json

각도 JS를 jQuery와는 달리 위의 헤더로 요청을 보냅니다.

  • 'application/x-www-form-urlencoded; charset=UTF-8'
+0

Q 편집에서 볼 수있는 것과 같은 것을 시도했습니다. 뭔가 빠졌습니다. 어떻게 문자열이 아닌 경우 분기 설명자를 전달할 수 있습니까? – soninob

0

당신은 다음과 같이 data에 자바 스크립트 객체를 전달해야 data: {param1: param1value, param2: param2value}

+0

나는 그것을 시도하고 위의 오류가있어, 내가 놓친 게 뭔가? – soninob

관련 문제