英文:
Sending multipart/mixed request with Jersey Client
问题
I will create a program framework that includes the necessary modules for an automated Chinese paper writing program, based on the information provided in the "lunwen.txt" file. Here is the initial code for the program framework:
class PaperGenerator:
def __init__(self):
# Initialize modules and configurations here
def generate_topic_candidates(self, user_input):
# Generate a list of research topic candidates based on user input
pass
def generate_keywords(self, selected_topic):
# Generate relevant keywords based on the selected research topic
pass
def generate_content(self, outline, keywords):
# Generate content for different sections of the paper using the outline and keywords
pass
def generate_abstract_and_conclusion(self, full_content):
# Generate abstract and conclusion based on the full content
pass
def search_literature(self, keywords):
# Search for relevant literature in specified academic databases
pass
def filter_and_categorize_literature(self, literature):
# Filter and categorize high-quality and relevant literature
pass
def format_citations_and_references(self, literature):
# Format citations and generate a reference list for the paper
pass
def optimize_content(self, generated_content):
# Optimize grammar, word choice, and logic of the generated content
pass
def polish_paragraphs(self, generated_content):
# Ensure logical flow and coherence of paragraphs
pass
def self_check_content(self, generated_content):
# Perform accuracy and originality checks on the generated content
pass
def format_paper(self, paper_content, journal_format):
# Format the paper according to specified journal/conference requirements
pass
def export_paper(self, formatted_paper, file_format):
# Export the completed paper in the specified file format (e.g., PDF)
pass
def insert_charts_and_data(self, content_with_sections, relevant_data):
# Insert relevant charts and data into the paper content
pass
def process_language(self, text):
# Perform various language processing tasks (tokenization, syntax analysis, etc.)
pass
def provide_user_interface(self):
# Provide a user interface for user interaction
pass
def perform_system_check(self):
# Continuously monitor and ensure proper functioning of all modules
pass
def search_related_papers(self, topic):
# Search for papers related to the given topic
pass
def generate_paper_outline(self):
# Generate an outline for the paper
pass
def write_paper_content(self, outline, keywords):
# Automatically generate paper content
pass
# Create an instance of the PaperGenerator class
paper_generator = PaperGenerator()
# Define the main program loop and interactions here
# Define how the program components interact and operate
# ...
Please note that this code provides a framework for implementing the various modules and functionalities outlined in the "lunwen.txt" file. You'll need to implement the actual methods within each module to achieve the desired functionality. Also, this is just a part of the entire code, and the complete code would involve further details and interactions between these modules.
英文:
I want to dialogue with this API service programmatically using Jersey (https://eclipse-ee4j.github.io/jersey/)
here's the Rest Controller implementation in Spring:
@PostMapping(
value = "/api/my-endpoint",
consumes = MediaType.MULTIPART_MIXED_VALUE)
public void enrichInvoice(@RequestPart("metadata") Map<String, Object> request,
@RequestPart("human") MultipartFile humanFile) {
log.info(String.format("received request:%n%s", request));
}
my client implementation would be like this
...
final Client client = ClientBuilder.newClient(new ClientConfig()
.register(MultiPartFeature.class)
.register(JacksonFeature.class)
);
final FileDataBodyPart filePart = new FileDataBodyPart("human",myFile()));
final BodyPart metadata = new BodyPart().entity(voBuilder.generateMetadata());
final MultiPart multiPartEntity = new MultiPart();
multiPartEntity.bodyPart(metadata, MediaType.APPLICATION_JSON_TYPE);
multiPartEntity.bodyPart(filePart);
final WebTarget target = client
.target("http://localhost:8080/api/my-endpoint");
final Entity<MultiPart> entity = Entity
.entity(multiPartEntity, multiPartEntity.getMediaType());
log.info(entity.toString());
final Response response = target
.request()
.post(entity);
log.info(String.format("%s", response.readEntity(String.class)));
response.close();
...
But i keep getting this error:
Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'metadata' is not present]
which is because the metadata part has to be named "metadata". and I cannot find a way to name it using BodyPart. I also tried using FormDataBodyPart to build the metadata
FormDataBodyPart metadataBodyPart = new FormDataBodyPart("metadata", metadata,
MediaType.APPLICATION_JSON_TYPE);
but with the same result.
Can you help me figure out what am I missing in the bodyPart definition?
Thanks
EDIT: here's the http request sent from my client implementation
Content-Type: multipart/mixed;boundary=Boundary_1_1972899462_1597045386454
User-Agent: Jersey/2.29 (HttpUrlConnection 11.0.8)
MIME-Version: 1.0
Host: localhost:8080
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 765
--Boundary_1_1972899462_1597045386454
Content-Type: application/json
{"value":"key"}
--Boundary_1_1972899462_1597045386454
Content-Type: application/octet-stream
Content-Disposition: form-data; filename="file.zip"; modification-date="Wed, 05 Aug 2020 16:52:52 GMT"; size=0; name="human"
--Boundary_1_1972899462_1597045386454--
]
答案1
得分: 0
以下是已翻译的代码部分:
final Path tempFile = Files.createTempFile("prefix", "suffix");
File fileMetadata = Files.write(tempFile.toAbsolutePath(), JsonUtils.toString(metadata).getBytes());
final FileDataBodyPart metadataBodyPart = new FileDataBodyPart(
"metadata",
fileMetadata,
MediaType.APPLICATION_JSON_TYPE);
final FileDataBodyPart human = new FileDataBodyPart("human", new File(humanReadableFile.getFileKey()));
try (final MultiPart multiPartEntity = new MultiPart()) {
multiPartEntity.bodyPart(metadataBodyPart);
multiPartEntity.bodyPart(human);
final Response response = client
.target("http://localhost:8080/api/my-endpoint")
.request()
.post(Entity.entity(multiPartEntity, multiPartEntity.getMediaType()));
log.debug(String.valueOf(response.getStatus()));
log.debug(response.readEntity(String.class));
}
请注意,已将 HTML 实体编码(")替换为正常的双引号。如果需要更多帮助,请告诉我。
英文:
The solution, even if not optimal, was to treat the metadata as a text file
final Path tempFile = Files.createTempFile("prefix", "suffix");
File fileMetadata = Files.write(tempFile.toAbsolutePath(), JsonUtils.toString(metadata).getBytes());
final FileDataBodyPart metadataBodyPart = new FileDataBodyPart(
"metadata",
fileMetadata,
MediaType.APPLICATION_JSON_TYPE);
final FileDataBodyPart human = new FileDataBodyPart("human", new File(humanReadableFile.getFileKey()));
try (final MultiPart multiPartEntity = new MultiPart()) {
multiPartEntity.bodyPart(metadataBodyPart);
multiPartEntity.bodyPart(human);
final Response response = client
.target("http://localhost:8080/api/my-endpoint")
.request()
.post(Entity.entity(multiPartEntity, multiPartEntity.getMediaType()));
log.debug(String.valueOf(response.getStatus()));
log.debug(response.readEntity(String.class));
}
In this way the request body has to parts named "metadata" and "human" as requested by the controller implementation and still maintain the multipart/mixed content-type.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论