3

私はグーグルで、これを何時間も機能させようとしています...問題は、サーバーがデータをJSONテキストとしてではなく受信していることです。これがPOJOです

package my.package;

import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement
    public class TestConfig {

        private String firmID;
        private String traderID;
        private String userID;

        public TestConfig() {};
    ...
    }

以下を含む Javascript クライアント:

    function callbackForTest(response) {
        console.log("Call to callbackForTest");
        if (response.state == "opening" && response.status == 200) {

            //push request data
            if (connectedEndpoint[0] == null) {
                console.log("[DEBUG] Connected endpoint for " + value + "is null!");
                //disable button
                $(value).attr('disabled','');
                $.atmosphere.unsubscribe();
                return false;
            }

            // push ( POST ) 
            connectedEndpoint[0].push(JSON.stringify(
                    {
                        operation       :   "RUN",
                        firmID          :   $('#firmID').val(),
                        userID          :   $('#userID').val(),
                        traderID        :   $('#traderID').val(),
                        protocol        :   $('#protocol').val(),
                        group1          :   
                    }
                ));
        }
    }

    function subscribeUrl(jobName, call, transport) {
        var location = subscribePath + jobName.id;
        return subscribeAtmosphere(location, call, transport);
    }

    function globalCallback(response) {
        if (response.state != "messageReceived") {
            return;
        }
    }

    function subscribeAtmosphere(location, call, transport) {
        var rq = $.atmosphere.subscribe(location, globalCallback, $.atmosphere.request = {
            logLevel : 'debug',
            transport : transport,
            enableProtocol: true,
            callback : call,
            contentType : 'application/json'
        });
        return rq;
    }

    function sendMessage(connectedEndpoint, jobName) {
        var phrase = $('#msg-' + jobName).val();
        connectedEndpoint.push({data: "message=" + phrase});
    }


    // Run Test handlers
    $("input[name='runButtons']").each(function(index, value){
        $(value).click(function(){

            //disable button
            $(value).attr('disabled','disabled');

            // connect (GET)
            connectedEndpoint[index] = subscribeUrl(value, callbackForTest, transport);
            });
        });

このスクリーンショットに示されているライブラリを含めました。

ライブラリ

そして、これは私のweb.xml(その一部)です

com.sun.jersey.api.json.POJOMappingFeature true

ジャージー リソース

@Path("/subscribe/{topic}")
@Produces({MediaType.APPLICATION_JSON, "text/html;charset=ISO-8859-1", MediaType.TEXT_PLAIN})
public class Subscriber {

    private static final Logger LOG = Logger.getLogger(Subscriber.class);

    @PathParam("topic")
    private Broadcaster topic;

    @GET
    public SuspendResponse<String> subscribe() {
        LOG.debug("GET - OnSubscribe to topic");
        SuspendResponse<String> sr = new SuspendResponse.SuspendResponseBuilder<String>().broadcaster(topic).outputComments(true)
                .addListener(new EventsLogger()).build();

        return sr;
    }

    @POST
    @Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN, MediaType.TEXT_HTML})
    @Broadcast
    public Broadcastable publish( TestConfig t) {
        LOG.debug("POST");
        String s = t.getFirmID();
        return new Broadcastable(s, "", topic);
    }

購読OKです。サーバーにプッシュしようとすると、次の例外が発生します。

A message body reader for Java class com.mx.sailcertifier.TestConfig, and Java type class com.mx.sailcertifier.TestConfig, and MIME media type text/plain was not found.

コンテンツ タイプを に設定すると、プレーン テキストが送信されるのはなぜapplication/jsonですか? JerseyリソースにJSONを読み取らせる正しい方法は何ですか?

4

1 に答える 1

2

私はついにこれを2つの変更で機能させました:

ここでサンプルを見た後、Tomcatの問題を解決するためにこれinit-paramをに追加しました。AtmosphereServletweb.xmltext/plain

<init-param>
     <param-name>org.atmosphere.websocket.messageContentType</param-name>
     <param-value>application/json</param-value>
</init-param>

Atmosphereのドキュメントに記載されている場所ではこれは見当たりませんでした。もしそうなら、それは多くの時間を節約できただろうが、ドキュメントに関しては、APIは残念ながらまとまりがなく、欠けている。

また、jersey-bundle jarを使用する必要がありました。これには、Jerseyに関連するすべてのものが含まれていることを確認してjersey-json.jarください。その後、うまくいきました!これが、同じまたは同様の問題で立ち往生している可能性のある他の誰かに役立つことを願っています。

于 2013-03-10T02:43:33.967 に答える