相关疑难解决方法(0)

什么是正确的JSON内容类型?

我一直在忙着JSON一段时间,只是把它作为文本推出它并没有伤害任何人(我知道),但我想开始正确地做事.

我见过这样的JSON内容类型很多所谓的"标准":

application/json
application/x-javascript
text/javascript
text/x-javascript
text/x-json
Run Code Online (Sandbox Code Playgroud)

但哪个是正确的,还是最好的?我认为它们之间存在安全性和浏览器支持问题.

我知道有一个类似的问题,如果REST API返回JSON,那么MIME类型是什么?,但我想要一个稍微有针对性的答案.

json content-type http-headers

9962
推荐指数
38
解决办法
265万
查看次数

如何使用Curl从终端/命令行发布JSON数据到测试Spring REST?

我使用Ubuntu并在其上安装了Curl.我想用Curl测试我的Spring REST应用程序.我在Java端编写了我的POST代码.但是,我想用Curl测试它.我正在尝试发布JSON数据.示例数据如下:

{"value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true}
Run Code Online (Sandbox Code Playgroud)

我用这个命令:

curl -i \
    -H "Accept: application/json" \
    -H "X-HTTP-Method-Override: PUT" \
    -X POST -d "value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true \
    http://localhost:8080/xx/xxx/xxxx
Run Code Online (Sandbox Code Playgroud)

它返回此错误:

HTTP/1.1 415 Unsupported Media Type
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 1051
Date: Wed, 24 Aug 2011 08:50:17 GMT
Run Code Online (Sandbox Code Playgroud)

错误描述如下:

服务器拒绝此请求,因为请求实体的格式不受所请求方法()的请求资源支持.

Tomcat日志:"POST/ui/webapp/conf/clear HTTP/1.1"415 1051

关于Curl命令的正确格式的任何想法?

编辑:

这是我的Java端PUT代码(我测试过GET和DELETE,它们有效)

@RequestMapping(method = RequestMethod.PUT)
public Configuration updateConfiguration(HttpServletResponse response, @RequestBody Configuration configuration) { //consider @Valid tag
    configuration.setName("PUT worked");
    //todo If error occurs …
Run Code Online (Sandbox Code Playgroud)

rest json curl spring-mvc http-headers

2606
推荐指数
24
解决办法
263万
查看次数

如何从Java Servlet返回JSON对象

如何从Java servlet返回JSON对象.

以前在使用servlet执行AJAX时,我返回了一个字符串.是否有需要使用的JSON对象类型,或者只是返回一个看起来像JSON对象的String,例如

String objectToReturn = "{ key1: 'value1', key2: 'value2' }";
Run Code Online (Sandbox Code Playgroud)

java json servlets

149
推荐指数
9
解决办法
43万
查看次数

如何使用curl将JSON发布到PHP

我可能会偏离基础,但我一直在尝试整个下午在这个凹陷的PHP框架教程中运行curl post命令.我不明白的是PHP应该如何解释我的POST,它总是作为一个空数组出现.

curl -i -X POST -d '{"screencast":{"subject":"tools"}}'  \
      http://localhost:3570/index.php/trainingServer/screencast.json
Run Code Online (Sandbox Code Playgroud)

(那里的斜线只是为了让我看起来不像白痴,但是我使用PHP 5.2在Windows上执行此操作,也在Linux服务器上执行此操作,与Linux卷曲相同的版本)

必须有一些我缺少的东西,因为它看起来非常简单,这篇文章不是正确的解释,如果是的话,一切都会很好.

这就是我得到的回报:

HTTP/1.1 409 Conflict
Date: Fri, 01 May 2009 22:03:00 GMT
Server: Apache/2.2.8 (Win32) PHP/5.2.6
X-Powered-By: PHP/5.2.6
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1

{"screencast":{"id":null,"subject":null,"body":null,
         "dataUrl":null,"dataMedium":null,"createdOn":null,"author":null}}

php rest post

108
推荐指数
4
解决办法
16万
查看次数

如果REST API返回JSON,那么MIME类型是什么?

我的REST API返回JSON.

我目前正在返回text/plain作为MIME类型,但感觉很有趣.我应该回来application/x-javascript还是其他类型?

第二个问题是关于错误条件的HTTP状态代码.如果我的REST API返回错误状态,我将返回JSON

{ result: "fail", errorcode: 1024, errormesg: "That sucked. Try again!" }
Run Code Online (Sandbox Code Playgroud)

HTTP状态代码应该保留在200 OK

api rest json http mime-types

67
推荐指数
5
解决办法
5万
查看次数

Spring Web MVC - 验证个人请求参数

我在Spring Web MVC 3.0中运行一个webapp,我有许多控制器方法,其签名大致如下:

@RequestMapping(value = "/{level1}/{level2}/foo", method = RequestMethod.POST)
public ModelAndView createFoo(@PathVariable long level1,
        @PathVariable long level2,
        @RequestParam("foo_name") String fooname,
        @RequestParam(value = "description", required = false) String description);
Run Code Online (Sandbox Code Playgroud)

我想添加一些验证 - 例如,description应该限制​​在一定长度或fooname应该只包含某些字符.如果此验证失败,我想向用户返回一条消息,而不是仅抛出一些未经检查的异常(如果我让数据渗透到DAO层,那么无论如何都会发生这种情况).我知道JSR303,但没有使用它,并不太明白如何在Spring上下文中应用它.

根据我的理解,另一种选择是绑定@RequestBody到整个域对象并在那里添加验证约束,但是目前我的代码被设置为接受如上所示的各个参数.

使用此方法将验证应用于输入参数的最直接方法是什么?

java validation spring spring-mvc

48
推荐指数
3
解决办法
7万
查看次数

Spring 3.0使用jackson消息转换器进行JSON响应

我将我的messageconverter配置为杰克逊的

class Foo{int x; int y}
Run Code Online (Sandbox Code Playgroud)

并在控制器中

@ResponseBody
public Foo method(){
   return new Foo(3,4)
}
Run Code Online (Sandbox Code Playgroud)

从那个我期望从服务器返回一个JSON字符串{x:'3',y:'4'},没有任何其他配置.但得到我的ajax请求的404错误响应

如果使用@ResponseBody注释该方法,则将返回类型写入响应HTTP正文.返回值将使用HttpMessageConverters转换为声明的方法参数类型.

我错了吗 ?或者我应该使用序列化程序将我的响应对象转换为Json字符串,然后将该字符串作为响应返回.(我可以正确地进行字符串响应)或者我应该进行其他配置吗?比如为Foo类添加注释

这是我的conf.xml

<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
Run Code Online (Sandbox Code Playgroud)

  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
  <list>
    <ref bean="jacksonMessageConverter"/>
  </list>
</property>
Run Code Online (Sandbox Code Playgroud)

java spring json spring-mvc jackson

24
推荐指数
2
解决办法
7万
查看次数

将JSON发布到REST API

我正在创建一个接受JSON请求的REST API.

我正在使用CURL测试它:

curl -i -POST -H 'Accept: application/json' -d '{"id":1,"pan":11111}' http://localhost:8080/PurchaseAPIServer/api/purchase
Run Code Online (Sandbox Code Playgroud)


但是得到以下错误:

HTTP/1.1 415 Unsupported Media Type
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 1051
Date: Wed, 25 Apr 2012 21:36:14 GMT

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().
Run Code Online (Sandbox Code Playgroud)



调试时,它甚至不会进入我在控制器中的创建操作.

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.app.model.Purchase;
import com.app.service.IPurchaseService; …
Run Code Online (Sandbox Code Playgroud)

rest spring json curl jackson

16
推荐指数
3
解决办法
9万
查看次数

JSON2与jquery-json

这可能是一个简单的问题,我似乎找不到可靠的答案.

为什么选择JSON2而不是jquery-json插件(http://code.google.com/p/jquery-json/)?鉴于Web应用程序正在使用jQuery开始.

每个人都在写JSON2对本机实现有多么重要; 好吧,jquery-json也是如此.我欢迎链接到博客,文章和示例.但是,我正在寻找一个强有力的答案,哪个更好用,为什么.

javascript jquery json jquery-plugins

9
推荐指数
2
解决办法
1万
查看次数

Spring REST | MappingJacksonHttpMessageConverter生成无效的JSON

我用Spring实现了一个RESTful Web服务.该服务基于Accept标头以XML或JSON响应.这是context.xml映射:

  <bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"/>
  <bean id="xmlMessageConverter"
        class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
    <constructor-arg ref="xstreamMarshaller"/>
    <property name="supportedMediaTypes" value="application/xml"/>
  </bean>

  <bean id="jsonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="prefixJson" value="false"/>
    <property name="supportedMediaTypes" value="application/json"/>
  </bean>

  <bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
      <util:list id="beanList">
        <ref bean="xmlMessageConverter"/>
        <ref bean="jsonHttpMessageConverter"/>
      </util:list>
    </property>
  </bean>
Run Code Online (Sandbox Code Playgroud)

这是我的控制器方法:

@Controller
@RequestMapping(value = "/entityService")
class RestfulEntityService {

  @Resource
  private EntityService entityService;

  @ResponseBody
  @RequestMapping(value = "/getAllEntities", method = RequestMethod.GET)
  public List<Entity> getAllEntities() {
    return entityService.getAllEntities();
  }
}
Run Code Online (Sandbox Code Playgroud)

XML响应是有效的,但是,当客户端将Accept标头设置为application/json时,响应是无效的JSON.

这是JSON响应示例:

[{"id":3,"attributes":[{"id":18,"attributeValue":null,"attributeName":"mobile","attributeType":"varchar(40)","entity":{"id":3,"attributes":[{"id":18,"attributeValue":null,"attributeName":"mobile","attributeType":"varchar(40)","entity":{"id":3,"attributes":[{"id":18,"attributeValue":null,"attributeName":"mobile","attributeType":"varchar(40)","entity":{"id":3,"attributes": ..... repeats for a while and then stops..
Run Code Online (Sandbox Code Playgroud)

java rest spring json

6
推荐指数
1
解决办法
1万
查看次数