来自Java Application Client的Jersey POST请求

时间:2016-05-02 12:27:54

标签: java jersey jersey-client

我有一个Java应用程序客户端,我想向服务器发送一个帖子请求。 这是我的SecretaryResource POST服务

@POST
 @Consumes(MediaType.APPLICATION_XML)
 public Secretary addSecretary(Secretary sec)
 {
      return secService.addSecretary(sec);

 }

这是我的客户端方法:

public boolean addSecretary()
    {
       Client client = ClientBuilder.newClient();
        WebTarget target = client.target("http://localhost:8080/ClinicManager/webapi/secretary");

Form form = new Form();
form.param("id","hardcoded");
form.param("password","hardcoded");
form.param("name", "hardcodedname");

Secretary bean =
target.request(MediaType.APPLICATION_XML)
   .post(Entity.entity(form,MediaType.APPLICATION_FORM_URLENCODED_TYPE),
         Secretary.class);


return true;


    }

我得到了这个例外

Exception in thread "AWT-EventQueue-0" javax.ws.rs.NotSupportedException: HTTP 415 Unsupported Media Type
at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1017)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:816)
at org.glassfish.jersey.client.JerseyInvocation.access$700(JerseyInvocation.java:92)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:700)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:696)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:448)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:349)
at clinicmanagerclient.controller.SecretaryDOM.addSecretary(SecretaryDOM.java:104)
at clinicmanagerclient.view.SecretaryCRUD.jButton2ActionPerformed(SecretaryCRUD.java:129)
at clinicmanagerclient.view.SecretaryCRUD.access$100(SecretaryCRUD.java:21)
at clinicmanagerclient.view.SecretaryCRUD$3.actionPerformed(SecretaryCRUD.java:79)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我也试过这种方式。仍然不起作用:

 Client client = ClientBuilder.newClient( new ClientConfig().register( LoggingFilter.class ) );
WebTarget webTarget = client.target("http://localhost:8080").path("/ClinicManager/webapi/secretary");

Secretary sec = new Secretary();
sec.setId(100);
sec.setName("David Feezor");
sec.setPassword("parola");

Invocation.Builder invocationBuilder =  webTarget.request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.post(Entity.entity(sec, MediaType.APPLICATION_JSON));

我用Postman测试了POST服务,它工作正常。错误的部分来自客户端。请帮帮我!

1 个答案:

答案 0 :(得分:1)

我认为问题在于您的内容类型不匹配。服务器接受xml(MediaType.APPLICATION_XML又称application/xml),但您发布的表单数据编码为MediaType.APPLICATION_FORM_URLENCODED_TYPE又名application/x-www-form-urlencoded

我猜你需要决定一种格式。你如何做到这一点以及你使用的转换取决于你。