2016-10-13 3 views
0

내 앱에서 일부 게시물 기능을 구현하려고합니다.AngularJS 400 잘못된 요청

나는 다음 포스트 방법을 가지고있다 :

@POST 
@Path("/restrict-single-ip") 
public Response RestrictSingleIp(@Valid RestrictLoginAttemptIpRequest requestData, @Context HttpRequest request){ 
     return Response.status(200).build(); 
} 

RestrictLoginAttemptIpRequest 클래스를 상속 한 필드 (dataOwnerId) 유형 :

restrictLoginAttemptsFromSingleIp: function (id, userId) { 
    var serviceUri = baseServicesUrlService.getBaseServicesUrl() + "/employee-service/restrict-single-ip"; 
    return $http.post(serviceUri, {restrictLoginAttemptIp: {loginAttemptIds: [id]}, dataOwnerId: userId}); 
} 

내 서버 측 최대 절전 모드 유효성 검사 RESTEasy가 3.0.4을 사용하고 있습니다 Long에서 PostBase :

public class RestrictLoginAttemptIpRequest extends PostBase { 
    private RestrictLoginAttemptIp restrictLoginAttemptIp; 

    public RestrictLoginAttemptIp getRestrictLoginAttemptIp() { 
     return restrictLoginAttemptIp; 
    } 

    public void setRestrictLoginAttemptIp(RestrictLoginAttemptIp restrictLoginAttemptIp) { 
     this.restrictLoginAttemptIp = restrictLoginAttemptIp; 
    } 
} 

RestrictLoginAttemptIp 클래스 : 내가 400 잘못된 요청 오류 때 얻을 이유

{restrictLoginAttemptIp={loginAttemptIds=[328]}, dataOwnerId=8} 

누군가가 설명해 주시겠습니까 : 나는 괜찮을 것 같다 POST 요청에서 다음 데이터 문자열을 얻을

package blah; 

import org.hibernate.validator.constraints.NotEmpty; 

import java.util.List; 

public class RestrictLoginAttemptIp { 
    @NotEmpty(message = "blah") 
    private List<Long> loginAttemptIds; 

    public List<Long> getLoginAttemptIds() { 
     return loginAttemptIds; 
    } 

    public void setLoginAttemptIds(List<Long> loginAttemptIds) { 
     this.loginAttemptIds = loginAttemptIds; 
    } 
} 

그 기능을 호출합니까?

Long 데이터 유형이 원인입니까? 어떻게해야할까요? Javascript로 Longs로 표시해야합니까?

답변

0

4 시간 후에 Ok 문제가 발생했습니다.

보안 요격기에서 POST 데이터 (권한 문제 해결)를 읽고있는 경우입니다. RESTEasy에서 POST 데이터를 읽는 것은 약간의 까다 롭습니다. 그것은 내 AngularJS와 요격에 고개를 다음 코드

String result = IOUtils.toString(requestContext.getEntityStream()); 
ObjectMapper mapper = new ObjectMapper(); 
Object obj = mapper.readValue(result, Object.class); 

에서 생각처럼 내가 아파치 IOUtils (https://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/IOUtils.html)을 사용하십시오 LinkedHashMap을 만들려면 (모든 요청의 헤더에 뭔가를 퍼 팅에 대 한 예를 들어 사용) 및 서버가 입력 스트림을 읽을 수 없다는 것을 알아 냈습니다 : java.io.ioexception no content to map to object due to end of input.

결국 문제는 내가 ContainerRequestContextEntityStream을 읽은 후 비어있는 것입니다. 해결책은 POST 데이터를 읽은 후 다시 채우는 것입니다. 이런 식으로 뭔가 :

private LinkedHashMap getPostData(ContainerRequestContext requestContext) { 
    Object obj = null; 

    try { 
     String result = IOUtils.toString(requestContext.getEntityStream()); 
     ObjectMapper mapper = new ObjectMapper(); 
     obj = mapper.readValue(result, Object.class); 

     //IMPORTANT: After you can get the entity stream only once. After reading the entity stream is empty 
     //so the JSON parser cannot convert EMPTY entity stream into any object. To avoid strange errors (like 400 Bad Request) 
     //you have to convert the string back to input stream and rewrite the empty entity stream. 
     InputStream stream = IOUtils.toInputStream(result); 
     requestContext.setEntityStream(stream); 

     System.out.println(obj); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return (LinkedHashMap) obj; 
} 

P. S. ObjectMapper 잭슨

에서 온다