WebサービスとSpringブートが初めてです。私は現在、テストケースを書いているサービスを書いています。SpringブートテストケースでAppconfigプロパティにアクセスする方法
私のアプリケーションは、SOAP要求を取得し、本文を解析し、内容をデータベースに保存します。 私のテストケースはこのサービスをテストします。
私がアプリケーションを実行し、郵便配達所からリクエストを送信すると、問題なく実行されます。しかし、私がサービスメソッドをテストケースから呼び出すと、JaxBcontextのnullポインタが返されます。
私のAppConfig.java(@Configurationと@Beanアノテーションの付いたBeanです)でJaxbcontextを宣言しましたが、jaxbcontextを使用するために@autowireがあります。
わかりやすくするためにコードスニペットを貼り付けました。私がここで間違ってやっていることを教えてください。
私のテストケース
public class ReferralExchangeEndpointTest {
ReferralExchangeEndpoint referralExchangeEndpoint = new ReferralExchangeEndpoint();
JAXBContext jbcTest;
Marshaller marshaller;
Unmarshaller unmarshaller;
public ReferralExchangeEndpointTest() throws JAXBException {
}
@Before
public void setUp() throws Exception {
jbcTest = JAXBContext.newInstance(
"our app schema"); // this is working fine, I have replaced schema with this text for posting it in stack.
ObjectFactory factory = new ObjectFactory();
marshaller = jbcTest.createMarshaller();
unmarshaller = jbcTest.createUnmarshaller();
}
@Test
public void send() throws Exception {
File payload = new File("payload.xml");
Object x = unmarshaller.unmarshal(payload);
JAXBElement jbe = (JAXBElement) x;
System.out.println(jbe.getName());
Object test = jbe.getValue();
SendRequestMessage sendRequestMessage = (SendRequestMessage) jbe.getValue();
// Method in test.
referralExchangeEndpoint.send(sendRequestMessage);
}
}
私のサービスクラス
@Endpoint
public class ReferralExchangeEndpoint {
public static final Logger logger = LoggerFactory.getLogger(ReferralExchangeEndpoint.class);
@Autowired
private JAXBContext jaxbContext;
@Autowired
.
.
.
private Form parseBody(String payLoadBody) {
try {
Unmarshaller um = jaxbContext.createUnmarshaller();
return (Form) um.unmarshal(new StringReader(payLoadBody));
} catch (Exception e) {
throw new RuntimeException("Failed to extract the form from the payload body", e);
}
}
私のAppConfigファイル
@Configuration
public class AppConfig {
@Bean
public JAXBContext jaxbContext() throws JAXBException {
return
JAXBContext.newInstance("packagename");
}
@Bean public MessagingService messagingService() {
return new MessagingService();
}
}
ありがとう。 Kavitha。
あなたの設定はスレッドセーフされていません。リファクタリングをお勧めし、Spring oxm(https://docs.spring.io/spring-ws/site/reference/html/oxm.html)でこれを設定してください。 1つのリクエストでローカルでテストしていると思われるので、おそらく同時実行性の問題は経験していないでしょう。 –
ダーレンありがとう、しかし、それを使用して、私のアプリケーションのために私のAppConfig属性をインスタンス化することができるという問題を解決するだろうか?それとも、それを稼働させるために他の何かをしなければならないのですか? – Kavitha
ダレンに感謝します。私はOXMの詳細を読んで、エンドポイントの実装に使用します。 – Kavitha