HTTP 415不支持的媒体类型(HTTP 415 Unsupported Media Type)
我创建了一个示例Web服务来进行调用。
我正在使用Jersey JAX-RS和Maven。
web.xml中
<servlet> <servlet-name>provider-serlvet</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>org.is.ws.provider.rest.ProviderAggregateApplication</param-value> </init-param> <init-param> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>provider-serlvet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
的pom.xml
<!-- Jersey jars --> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.19</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.19</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.19</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-servlet</artifactId> <version>1.19</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>1.18</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency>
方法调用:
@POST @Path(RESOURCE_PATH) @Produces("text/plain") public String getPCP(){ return "Hello"; }
原始要求:
POST http://localhost:9080/sep/pas/getPCP HTTP/1.1 Connection: close Accept-Encoding: gzip,deflate Content-Type: application/json Content-Length: 69 Host: localhost:9080 User-Agent: Apache-HttpClient/4.1.1 (java 1.5) { "patientID": "1234567890", "patientIDType": "XYZ" }
我已经提到了以下文章
REST Webservice返回415 - 不支持的媒体类型
但我仍然有问题。
I have created a sample web service to make a post call.
I am using Jersey JAX-RS, and Maven.
web.xml
<servlet> <servlet-name>provider-serlvet</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>org.is.ws.provider.rest.ProviderAggregateApplication</param-value> </init-param> <init-param> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>provider-serlvet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
POM.XML
<!-- Jersey jars --> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.19</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.19</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.19</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-servlet</artifactId> <version>1.19</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>1.18</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency>
Method call:
@POST @Path(RESOURCE_PATH) @Produces("text/plain") public String getPCP(){ return "Hello"; }
Raw request:
POST http://localhost:9080/sep/pas/getPCP HTTP/1.1 Connection: close Accept-Encoding: gzip,deflate Content-Type: application/json Content-Length: 69 Host: localhost:9080 User-Agent: Apache-HttpClient/4.1.1 (java 1.5) { "patientID": "1234567890", "patientIDType": "XYZ" }
I have referred to following article
REST Webservice returning 415 - Unsupported Media Type
But still i'm having issue.
原文:https://stackoverflow.com/questions/30825375
最满意答案
尝试这个:
Sub GetData() Dim strPath As String Dim strFileName As String Dim strFileExtension As String Dim strFullName As String dim objExcel As Object, objWorkbook As Object strPath = "file path here" strFileName = "file name here" strFileExtension = "Extension here" strFullName = strPath & strFileName & strFileExtension Set objExcel = CreateObject("Excel.Application") Set objWorkbook = objExcel.Workbooks.Open(strFullName) 'Set the text of the cell from Excel to the cell in the specified table in 'Word (the second table in this instance) ActiveDocument.Tables(1).Cell(2, 2).Range.Text = objWorkbook.Sheets("Sheet1").Cells(2, 1) 'Close Excel bits objWorkbook.Close objExcel.Quit Set objWorkbook = Nothing Set objExcel = Nothing End Sub
顺便说一下,如果使用上面使用的
CreateObject
函数,则不需要添加引用。你有几个问题:
- 没有定义(设置)
objExcel
应用程序- 正确执行后没有关闭(清理)
- Excel对象的不必要的公共声明
Try this:
Sub GetData() Dim strPath As String Dim strFileName As String Dim strFileExtension As String Dim strFullName As String dim objExcel As Object, objWorkbook As Object strPath = "file path here" strFileName = "file name here" strFileExtension = "Extension here" strFullName = strPath & strFileName & strFileExtension Set objExcel = CreateObject("Excel.Application") Set objWorkbook = objExcel.Workbooks.Open(strFullName) 'Set the text of the cell from Excel to the cell in the specified table in 'Word (the second table in this instance) ActiveDocument.Tables(1).Cell(2, 2).Range.Text = objWorkbook.Sheets("Sheet1").Cells(2, 1) 'Close Excel bits objWorkbook.Close objExcel.Quit Set objWorkbook = Nothing Set objExcel = Nothing End Sub
Btw you don't need to add references if you use the
CreateObject
function as used above.You had a couple of issues:
- Did not define (set) the
objExcel
application- Weren't closing (cleaning) after execution properly
- Unnecessary Public declarations of Excel objects
相关问答
更多-
Sub RemoveFooterRows(theFile) Dim found As Range Set found = Workbooks(theFile).Worksheets(1).Columns(1).Find _ ("Summary", Range("A1"), xlValues, xlPart, xlByRows, xlNext, False, , False) MsgBox ("val is " & found.Row) End Sub 您需要“ ...
-
Win8的运行时错误91(runtime error 91 with Win8)[2022-07-19]
出现错误91,因为没有设置对象,所以我会确保ActiveWorkbook确实添加了工作表。 您可能想尝试一下,看看它是否仍然会抛出错误。 Sub Worksheet_UnLock() Dim s as Worksheet For Each s In ActiveWorkbook.Sheets s.Unprotect Password:=myPassword Next End Sub Error 91 occurs because an object is not ... -
您可以将If Not IsEmpty(Table1.UsedRange) Then语句添加到代码中。 如果工作表完全为空,这将阻止代码运行。 如果您需要更多帮助,请发表评论。 Sub CreateBasicWordReport() 'Create word doc automatically Dim wApp As Word.Application Dim SaveName As String Set wApp = New Word.Application With wApp 'Make word v ...
-
这样做你想要的,但你需要优化初始范围选择,以确保你没有拿起任何空值。 Sub SiteAccess() Dim mySheet As Worksheet, myOtherSheet As Worksheet, myBook As Workbook 'Define your workbooks and worksheets as variables Set myBook = Excel.ActiveWorkbook Set mySheet = myBook.Sheets("SiteAccessRepo ...
-
这是一个直接告诉你行号的函数。 这与您在构建函数中使用以获得最大值然后再次找到它的方法相反! Function GetMaxRow(ByRef myRange As Range) As Long Dim aCell As Range Dim maxVal As Double Dim maxRow As Long maxVal = Application.WorksheetFunction.Min(myRange) maxRow = 0 For Each ...
-
尝试这个: Sub GetData() Dim strPath As String Dim strFileName As String Dim strFileExtension As String Dim strFullName As String dim objExcel As Object, objWorkbook As Object strPath = "file path here" strFileName = "file name here" strFileEx ...
-
似乎Selection随时无效,即在运行宏时,选择以某种方式无效,例如通过用户点击Excel窗口或类似的东西。 尝试直接使用图形或对象的Left属性,而不使用Selection. : x = 1 While x < 4 With wb.Sheets("SheetX").ChartObjects(x) .Parent.Height = 500 ' resize .Parent.Width = 1200 ' resize .Parent.Top = 100 ...
-
Excel VBA运行时错误91(Excel VBA Run-Time Error 91)[2022-05-05]
如果新表不包含任何数据,则其DataBodyRange为Nothing。 向表中添加任何数据时,DataBodyRange设置为一个范围,即使稍后删除数据,它也将保持初始化状态。 在您的情况下,最简单的解决方案可能是在执行任何操作之前测试DataBodyRange,即 If Not sht.ListObjects("Basic_Data_Table").DataBodyRange Is Nothing Then your code here End if 要么 If sht.ListObjects ... -
错误运行时91和429(Error runtime 91 and 429)[2022-03-16]
不确定为什么将'BuildSel'改为variant会使它工作,但是当没有找到匹配的列表项时,它所代表的代码没有错误检查 以下代码应该更适合使用: Private Sub BuildList_Click() Dim Ref As Worksheet: Set Ref = ThisWorkbook.Sheets("Ref") Dim BuildSel As Range With Ref Set BuildSel = .Range("B2", .Range("B" & R ... -
你在sub中做的第一件事是你清空刚刚通过的Argument(使用Set rs = Nothing ),然后再次使用rs 。 所以除非rs是一个类似Public的变量并且它填充了ConnectToDb , 在使用之前它仍然是空的,错误! 首先,尝试删除Set rs = Nothing ,如果还不够,您需要查看ConnectToDb ! 其次,你修改了sub中的Recordset,但你试着在外面使用它。 这里的问题是使用ByVal ,它传递了Object的引用副本,所以你不能修改初始对象,因为RecordSet ...