Newer
Older
stockManagement.pull / src / main / java / stockManagement / pull / Request.java
Shinji on 21 Jan 2023 1 KB プロジェクトの追加
package stockManagement.push;

import java.util.*;
import javax.ws.rs.*;
import javax.ws.rs.client.*;
import javax.ws.rs.core.*;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;

import pushPullRefactor.Getter;
import pushPullRefactor.Message;
import pushPullRefactor.Resource;
import pushPullRefactor.State;

import com.fasterxml.jackson.core.JsonProcessingException;


@Path("/request")
@Resource("request")
@Component
public class Request {
	private Client client = ClientBuilder.newClient();
	@State
	private Map.Entry<String, Integer> value;
	@PUT
	@Message({})
	public void req(@FormParam("num") int num, @FormParam("item") String item) throws JsonProcessingException {
		this.value = new AbstractMap.SimpleEntry<>(item, num);
		Map<String, Integer> stock_json = client.target("http://localhost:8080").path("/stock").request().get(HashMap.class);
		Map<String, Integer> stock = new HashMap<>();
		stock = stock_json;
		Form form = new Form();
		form.param("request", new ObjectMapper().writeValueAsString(this.value));
		form.param("stock", new ObjectMapper().writeValueAsString(stock));
		Entity<Form> entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);
		String result = client.target("http://localhost:8080").path("/shortage/request").request().post(entity, String.class);
		form = new Form();
		form.param("request", new ObjectMapper().writeValueAsString(this.value));
		form.param("stock", new ObjectMapper().writeValueAsString(stock));
		entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);
		result = client.target("http://localhost:8080").path("/deriver/request").request().put(entity, String.class);
	}
	@Produces(MediaType.APPLICATION_JSON)
	@GET
	@Getter
	public Map.Entry<String, Integer> getValue() {
		return value;
	}
}