【问题标题】:Restfull Services org.netbeans.rest.application.config.ApplicationConfig threw exceptionRestful Services org.netbeans.rest.application.config.Application Config 抛出异常
【发布时间】:2016-04-08 22:06:43
【问题描述】:

我有一个 Angular 客户端;其将数据发布到 java Rest Api。当我尝试发送字符串或整数数据时,没有任何问题。但是当我尝试发送 List 时,它给出了一个类似的错误;enter image description here

StandardWrapperValve[org.netbeans.rest.application.config.ApplicationConfig]:Servlet org.netbeans.rest.application.config.ApplicationConfig 的 Servlet.service() 抛出异常 java.lang.IllegalArgumentException:参数类型不匹配

还有我的 Java Api

@POST
@Path("/senddata")
@Produces("application/json")
public String sendData(List<Data> model) throws Exception {
    System.out.println("model : " + model);
    return "OK";
}

来自 Angularjs 的请求

   return $http.post('http://localhost:8080/Project/webresources/json/product/senddata', postData1, {
      headers: { 'Content-Type': 'application/json' }
    });

我的字符编码类

@WebFilter("*.xhtml")
public class CharacterEncodingFilter implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.getCharacterEncoding();
        response.getCharacterEncoding();
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }
}

谢谢

【问题讨论】:

    标签: java angularjs ajax rest


    【解决方案1】:

    您可以尝试为您的 List 创建一个包装器对象并在您的参数列表中使用它;

    public class DataWrapper { 
        private List<Data> dataList;
    ...
    }  
    
    
    @POST
    @Path("/senddata")
    @Produces("application/json")
    public String sendData(DataWrapper data) throws Exception {
    ...
    

    或者将@RequestParam(value="model") 注释添加到您现有的参数中。

    public String sendData(@RequestParam(value="model") List<Data> model) throws Exception
    

    【讨论】:

    • 如果您更新了一个有关如何实施您的建议的示例,您的答案会更有帮助。
    • 我同意@devlin
    猜你喜欢
    • 2017-06-15
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 2011-05-30
    • 1970-01-01
    • 2011-02-25
    相关资源
    最近更新 更多