我正在使用带有Jersey和Tomcat的JAX-RS Web应用程序。发布请求很好但是当我尝试使用JSON数据时,我得到一个HTTP状态415 - 不支持的媒体类型。
这是我的代码:
package com.sms.presentationT;
import java.io.Serializable;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import com.sms.userServices.*;
import com.sms.Registrations.Regitration;
import com.sms.persistenceT.*;
import com.sms.persistenceT.*;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
@Path("/user-servicesLogincheck1")
public class Check implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@POST
@Path("/LoginPagecheck1")
//@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Consumes(MediaType.APPLICATION_JSON)
//@Produces("application/json")
@Produces(MediaType.APPLICATION_JSON)
public Regitration loginUserInfo(@FormParam("logusertype") String logUserType, @FormParam("userNAME") String UserNAME,*emphasized text* @FormParam("PassWORD") String PAssWORD) throws ParseException
{
System.out.println(logUserType+UserNAME+PAssWORD);
LoginUserServiceCheck LUS = new LoginUserServiceCheck();
JSONObject jobj2 = new JSONObject();
jobj2 = LUS.LoginInputDatacheck(logUserType,UserNAME, PAssWORD);
System.out.println("ssssssssssssssssssssssssssss");
System.out.println("Presentation jobj"+jobj2);
String jsonText = jobj2.toJSONString();
System.out.println(jsonText);
JSONParser parser = new JSONParser();
JSONObject newJObject = null;
try {
newJObject = (JSONObject) parser.parse(jsonText);
} catch (ParseException e) {
e.printStackTrace();
}
//System.out.println(LUS.loginresponse);
//System.out.println(LUS.resultStatus);
/*
if(LUS.resultStatus)
{
System.out.println(LUS.loginresponse);
return LUS.loginresponse;
//return "UserName is valid";
}
else
{
return LUS.loginresponse;
}
*/
System.out.println(newJObject.get("Name"));
System.out.println(newJObject.get("Email"));
Regitration rg = new Regitration();
rg.setUserType(logUserType);
rg.setName((String)newJObject.get("Name"));
rg.setEmail((String)newJObject.get("Email"));
rg.setUserName((String)newJObject.get("userName"));
rg.setPassword((String)newJObject.get("password"));
rg.setConfirmPasssword((String)newJObject.get("password"));
rg.setBirthDay((String)newJObject.get("birthday"));
rg.setGender((String)newJObject.get("Gender"));
rg.setMobileNo((String)newJObject.get("mobileNo"));
rg.setCurrentAddress((String)newJObject.get("currentAddress"));
rg.setPermenantAddress((String)newJObject.get("PermenantAddress"));
rg.setCountry((String)newJObject.get("Country"));
rg.setState((String)newJObject.get("state"));
rg.setCity((String)newJObject.get("City"));
rg.setLandmark((String)newJObject.get("Landmark"));
rg.setZipCode((String)newJObject.get("ZipCode"));
return rg;
}
}
这是我的web.xml文件
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Restful Web Application</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.sms.presentationT</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>jersey-serlvet</servlet-name>
<url-pattern>/pages/rest/*</url-pattern>
</servlet-mapping>
这是我的POM.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mkyong.common</groupId>
<artifactId>RESTfulExample</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>RESTfulExample Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20141113</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>0.99</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
<build>
<finalName>RESTfulExample</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
答案 0 :(得分:0)
我想指出你不使用java命名约定。
您的REST端点需要表单数据,但您已指出它使用JSON。 您应该让REST端点使用表单数据,即 @Consumes(MediaType.APPLICATION_FORM_URLENCODED) 或者,您可以将 FormParam 方法参数更改为与您的JSON对象或String参数匹配的POJO,然后将您的JSON字符串反序列化为POJO。