2016-04-19 4 views
0

私はprotobufとjsonでwebserviceを作ろうとしています。 問題は、protoを構築するためにinputStreamを読み取る必要があることが必要なことです(少なくとも私はそれをやる別の方法はありません)。私springconfigurationにwebservice handling protobuf

public class ProtobufMessageConverter extends AbstractHttpMessageConverter<MyProto>{ 

    @Override 
    protected boolean supports(Class<?> aClass) { 
     return MyProto.class.equals(aClass); 
    } 

    @Override 
    protected MyProto readInternal(Class<? extends MyProto> aClass, HttpInputMessage httpInputMessage) 
      throws IOException, HttpMessageNotReadableException { 
     return MyProto.parseFrom(httpInputMessage.getBody()); 
    } 

    @Override 
    protected void writeInternal(MyProto proto, HttpOutputMessage httpOutputMessage) 
      throws IOException, HttpMessageNotWritableException { 
     OutputStream wr = httpOutputMessage.getBody(); 
     wr.write(proto.toByteArray()); 
     wr.close(); 
    } 
} 

を使用:

は私がいるProtobufのためのコンバータを作成し

@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages = "com.test") 
public class SpringMvcConfiguration extends WebMvcConfigurationSupport { 

    @Override 
    public void configureMessageConverters(List<HttpMessageConverter<?>> httpMessageConverters) { 
     httpMessageConverters.add(new ProtobufMessageConverter(new MediaType("application","octet-stream"))); 

     addDefaultHttpMessageConverters(httpMessageConverters); 
    } 
} 

マイコントローラ:

@RequestMapping(value = "/proto", method = {POST}, consumes = {MediaType.APPLICATION_OCTET_STREAM_VALUE}) 
@ResponseBody 
public MyProto openProto(@RequestHeader(value = "Host") String host, @RequestBody 
    MyProto strBody, HttpServletRequest httpRequest 
) throws InterruptedException { 
    return null; 
} 

問題は、私は、コントローラのように聞かせている場合ということですこれは、私のWebサービスがアプリケーション/オクテットストリームをサポートしていないためにエラーが発生します。

[メイン] INFO org.eclipse.jetty.server.ServerConnector - 73b05494 @スタートServerConnector {HTTP/1.1} {0.0.0.0:8180} org.springframework.web.HttpMediaTypeNotSupportedException:コンテンツタイプ「アプリケーション/ octet-私は@RequestBodyに文字列を入れる場合はorg.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:155)で ...

をサポートしていない「ストリーム、その後、私は中に入ります私の方法ですが、コンバータを使用していないようで、文字列をparseFrom関数でMyProtoにキャストすることはできません。

ご存知ですか?

答えて

0

私は実際にその答えを見つけました。 protobufをbyte []として考える必要があります。このタイプのHttpMessageConverterは既に存在します。したがって、ResponseBodyは

public byte[] openProto(@RequestHeader(value = "Host") String host, @RequestBody 
    byte[] strBody, HttpServletRequest httpRequest 
) throws InterruptedException { 
    return null; 
} 
である必要があります。